:root {
  --bg: #0b0d14;
  --panel: #141826;
  --panel-2: #1b2033;
  --line: #262c44;
  --text: #e6e9f5;
  --muted: #8b93b0;
  --faint: #5d6585;
  --accent: #5c8cff;
  --green: #34d399;
  --amber: #fbbf24;
  --red: #f87171;
  --user: #24305a;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior-y: contain;
}
button { font: inherit; color: inherit; }
a { color: var(--accent); }
#app { min-height: 100%; display: flex; flex-direction: column; }

/* ── верхняя панель ── */
.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + var(--safe-top)) 12px 10px;
  background: rgba(11, 13, 20, .92); backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.top h1 { font-size: 18px; margin: 0; font-weight: 650; flex: 1; min-width: 0; }
.top .sub { display: block; font-size: 12px; color: var(--muted); font-weight: 400; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top .ttl { flex: 1; min-width: 0; }
.top .ttl b { display: block; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn {
  width: 40px; height: 40px; flex: none; border-radius: 12px; border: 0;
  background: transparent; display: grid; place-items: center; cursor: pointer;
}
.icon-btn:active { background: var(--panel-2); }
.icon-btn svg { width: 22px; height: 22px; stroke: var(--text); fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.summary { display: flex; gap: 8px; padding: 10px 12px 2px; flex-wrap: wrap; }
.chip {
  font-size: 12px; padding: 4px 10px; border-radius: 999px; background: var(--panel); border: 1px solid var(--line); color: var(--muted);
}
.chip b { color: var(--text); font-weight: 600; }

.banner {
  margin: 10px 12px 0; padding: 12px; border-radius: var(--radius); background: #1e2a4d; border: 1px solid #34467e;
  display: flex; gap: 10px; align-items: center; font-size: 14px;
}
.banner span { flex: 1; }

/* ── карточки аккаунтов ── */
.list { padding: 10px 12px calc(24px + var(--safe-bottom)); display: flex; flex-direction: column; gap: 8px; }
.card {
  display: block; text-decoration: none; color: inherit;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 12px 11px; position: relative;
}
.card:active { background: var(--panel-2); }
.card .row { display: flex; align-items: center; gap: 8px; }
.card .name { font-weight: 650; font-size: 15px; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .num { color: var(--faint); font-weight: 500; font-size: 12px; margin-right: 2px; }
.card .st { font-size: 12px; color: var(--muted); white-space: nowrap; }
.card .title { color: var(--muted); font-size: 13px; margin-top: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .body { margin-top: 6px; font-size: 13.5px; color: #c4cae0; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card.idle { opacity: .72; }
.card.done.unseen { border-color: #1f6b52; }
.card.ask { border-color: #8a6a12; }
.badge { font-size: 11px; padding: 1px 7px; border-radius: 999px; background: var(--green); color: #052e22; font-weight: 700; }
.badge.ask { background: var(--amber); color: #3b2a00; }

.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--faint); flex: none; }
.dot.running { background: var(--accent); box-shadow: 0 0 0 0 rgba(92, 140, 255, .7); animation: pulse 1.6s infinite; }
.dot.done { background: var(--green); }
.dot.aborted { background: var(--amber); }
.dot.error { background: var(--red); }
.dot.ask { background: var(--amber); animation: pulse-amber 1.6s infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(92,140,255,.7); } 70% { box-shadow: 0 0 0 8px rgba(92,140,255,0); } 100% { box-shadow: 0 0 0 0 rgba(92,140,255,0); } }
@keyframes pulse-amber { 0% { box-shadow: 0 0 0 0 rgba(251,191,36,.7); } 70% { box-shadow: 0 0 0 8px rgba(251,191,36,0); } 100% { box-shadow: 0 0 0 0 rgba(251,191,36,0); } }

.act { display: flex; gap: 6px; align-items: baseline; font-size: 12.5px; color: var(--muted); margin-top: 6px; min-width: 0; }
.act .ic { flex: none; width: 16px; text-align: center; }
.act .tx { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 12px; }
.act.msg .tx, .act.think .tx { font-family: inherit; }

/* ── список чатов аккаунта ── */
.section-h { padding: 14px 14px 4px; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--faint); }
.thread-item { display: flex; gap: 10px; align-items: center; padding: 12px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); text-decoration: none; color: inherit; }
.thread-item .t { flex: 1; min-width: 0; }
.thread-item .t b { display: block; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-item .t span { display: block; font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-item time { font-size: 12px; color: var(--faint); white-space: nowrap; }

.btn {
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text); border-radius: 12px;
  padding: 10px 14px; font-weight: 600; cursor: pointer; display: inline-flex; align-items: center; gap: 8px; justify-content: center;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.green { background: #0f5a43; border-color: #1f8a68; }
.btn.red { background: #5a1d1d; border-color: #8a2f2f; }
.btn:disabled { opacity: .5; }
.btn-row { display: flex; gap: 8px; padding: 0 12px; flex-wrap: wrap; }

/* ── чат ── */
.chat { flex: 1; padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 10px; }
.turn { display: flex; flex-direction: column; gap: 6px; }
.bubble { max-width: 92%; padding: 10px 12px; border-radius: 16px; word-wrap: break-word; overflow-wrap: anywhere; }
.bubble.user { align-self: flex-end; background: var(--user); border-bottom-right-radius: 6px; white-space: pre-wrap; }
.bubble.agent { align-self: stretch; max-width: 100%; background: var(--panel); border: 1px solid var(--line); border-bottom-left-radius: 6px; }
.bubble.err { border-color: #7a2c2c; background: #2a1416; }
.thumbs { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.thumbs img { width: 72px; height: 72px; object-fit: cover; border-radius: 8px; }
.meta { font-size: 11.5px; color: var(--faint); display: flex; gap: 8px; align-items: center; }
.meta.right { align-self: flex-end; }
.toggle { background: none; border: 0; color: var(--muted); font-size: 12.5px; padding: 2px 0; cursor: pointer; text-align: left; }
.toggle:active { color: var(--text); }

.md { font-size: 14.5px; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: .5em 0; }
.md ul, .md ol { margin: .4em 0; padding-left: 1.3em; }
.md li { margin: .15em 0; }
.md code { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 12.5px; background: #0e1120; padding: 1px 5px; border-radius: 5px; }
.md pre { background: #0e1120; padding: 10px; border-radius: 10px; overflow-x: auto; }
.md pre code { padding: 0; background: none; }
.md img { max-width: 100%; border-radius: 10px; display: block; margin: 6px 0; }
.md table { border-collapse: collapse; display: block; overflow-x: auto; font-size: 13px; }
.md th, .md td { border: 1px solid var(--line); padding: 4px 8px; }
.md h1, .md h2, .md h3 { font-size: 15.5px; margin: .8em 0 .3em; }
.md blockquote { margin: .5em 0; padding-left: 10px; border-left: 3px solid var(--line); color: var(--muted); }
.md a.file::before { content: "📄 "; }
.md a.img::before { content: "🖼 "; }

.items { border-left: 2px solid var(--line); margin-left: 6px; padding-left: 10px; display: flex; flex-direction: column; gap: 6px; }
.item { font-size: 12.5px; color: var(--muted); }
.item pre { margin: 4px 0 0; white-space: pre-wrap; word-break: break-word; background: #0e1120; border-radius: 8px; padding: 8px; font-size: 11.5px; max-height: 240px; overflow: auto; color: #b9c0da; }
.item summary { display: flex; gap: 6px; align-items: baseline; cursor: pointer; list-style: none; }
.item summary::-webkit-details-marker { display: none; }
.item .cmd { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; color: #c9d1ee; }
.item summary > span:not(.cmd) { flex: none; }
.item details[open] .cmd { color: var(--muted); }
.item pre.full-cmd { color: #c9d1ee; }
.item .ok { color: var(--green); }
.item .bad { color: var(--red); }
.item.agent { color: #c4cae0; }

.live { background: #121a33; border: 1px solid #2b3b72; border-radius: var(--radius); padding: 10px 12px; }
.live .h { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 14px; }
.live .acts { margin-top: 4px; }

.ask-card { background: #2a2210; border: 1px solid #8a6a12; border-radius: var(--radius); padding: 12px; }
.ask-card .h { font-weight: 650; margin-bottom: 4px; }
.ask-card pre { white-space: pre-wrap; word-break: break-word; font-size: 12.5px; margin: 6px 0 10px; color: #f3e2b0; font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; }

.queue { background: var(--panel); border: 1px dashed var(--line); border-radius: var(--radius); padding: 8px 12px; font-size: 13px; color: var(--muted); }
.queue .q { display: flex; gap: 8px; align-items: center; padding: 4px 0; }
.queue .q span { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--text); }

.composer {
  position: sticky; bottom: 0; z-index: 4;
  padding: 8px 10px calc(8px + var(--safe-bottom));
  background: rgba(11, 13, 20, .95); backdrop-filter: blur(10px); border-top: 1px solid var(--line);
}
.composer .row { display: flex; gap: 6px; align-items: flex-end; }
.composer textarea {
  flex: 1; resize: none; min-height: 42px; max-height: 40vh; padding: 10px 12px; border-radius: 14px;
  border: 1px solid var(--line); background: var(--panel); color: var(--text); font: inherit; line-height: 1.35;
}
.composer textarea:focus { outline: none; border-color: #3d4c86; }
.composer .send { width: 42px; height: 42px; border-radius: 50%; border: 0; background: var(--accent); display: grid; place-items: center; flex: none; cursor: pointer; }
.composer .send:disabled { background: var(--panel-2); }
.composer .send svg, .composer .attach svg { width: 20px; height: 20px; stroke: #fff; fill: none; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.composer .attach { width: 42px; height: 42px; border-radius: 50%; border: 1px solid var(--line); background: var(--panel); display: grid; place-items: center; flex: none; cursor: pointer; }
.composer .attach svg { stroke: var(--muted); }
.composer .hint { font-size: 11.5px; color: var(--faint); margin: 0 4px 6px; }
.composer .pending { display: flex; gap: 6px; margin-bottom: 6px; flex-wrap: wrap; }
.composer .pending div { position: relative; }
.composer .pending img { width: 56px; height: 56px; object-fit: cover; border-radius: 8px; display: block; }
.composer .pending button { position: absolute; top: -6px; right: -6px; width: 20px; height: 20px; border-radius: 50%; border: 0; background: #000c; font-size: 12px; line-height: 20px; padding: 0; cursor: pointer; }

/* ── вход, настройки ── */
.center { flex: 1; display: grid; place-items: center; padding: 24px 16px; }
.login { width: 100%; max-width: 360px; text-align: center; }
.login img { width: 84px; height: 84px; }
.login h2 { margin: 12px 0 4px; }
.login p { color: var(--muted); margin: 0 0 18px; font-size: 14px; }
.login input, .field input {
  width: 100%; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel);
  color: var(--text); font: inherit; font-size: 16px; margin-bottom: 10px;
}
.login .btn { width: 100%; }
.err-text { color: var(--red); font-size: 13px; min-height: 18px; margin-top: 8px; }

.settings { padding: 8px 12px calc(24px + var(--safe-bottom)); display: flex; flex-direction: column; gap: 10px; }
.box { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; }
.box h3 { margin: 0 0 6px; font-size: 15px; }
.box p { margin: 0 0 10px; color: var(--muted); font-size: 13.5px; }
.switch-row { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-top: 1px solid var(--line); }
.switch-row:first-of-type { border-top: 0; }
.switch-row span { flex: 1; }
.switch { appearance: none; width: 44px; height: 26px; border-radius: 13px; background: var(--panel-2); border: 1px solid var(--line); position: relative; cursor: pointer; flex: none; }
.switch::after { content: ""; position: absolute; top: 2px; left: 2px; width: 20px; height: 20px; border-radius: 50%; background: var(--muted); transition: left .15s; }
.switch:checked { background: #1f4fbf; border-color: #2b5fd6; }
.switch:checked::after { left: 20px; background: #fff; }

.empty { color: var(--muted); text-align: center; padding: 30px 16px; font-size: 14px; }
.spinner { width: 16px; height: 16px; border: 2px solid #3d4c86; border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }
.loading { display: flex; justify-content: center; padding: 30px; }

#toast {
  position: fixed; left: 50%; bottom: calc(84px + var(--safe-bottom)); transform: translateX(-50%) translateY(20px);
  background: #2a3150; color: var(--text); padding: 10px 14px; border-radius: 12px; font-size: 14px; max-width: calc(100% - 32px);
  opacity: 0; pointer-events: none; transition: all .2s; z-index: 50; box-shadow: 0 6px 24px #0008;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#lightbox { position: fixed; inset: 0; background: #000e; z-index: 60; display: grid; place-items: center; padding: 12px; }
#lightbox[hidden] { display: none; }
#lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; }

.menu { position: absolute; right: 10px; top: calc(56px + var(--safe-top)); background: var(--panel-2); border: 1px solid var(--line); border-radius: 12px; padding: 6px; z-index: 20; min-width: 220px; box-shadow: 0 10px 30px #000a; }
.menu button { display: block; width: 100%; text-align: left; background: none; border: 0; padding: 10px 12px; border-radius: 8px; cursor: pointer; }
.menu button:active { background: var(--panel); }

@media (min-width: 720px) {
  .list, .chat, .settings, .composer .row, .summary { max-width: 760px; margin-left: auto; margin-right: auto; width: 100%; }
}
