:root {
  --bg: #0a0e12;
  --surface: #10161d;
  --surface-2: #16202a;
  --surface-3: #1c2833;
  --border: rgba(255, 255, 255, .08);
  --border-strong: rgba(255, 255, 255, .14);
  --text: #eaf1f7;
  --dim: #93a4b3;
  --accent: #37e3a4;
  --accent-soft: rgba(55, 227, 164, .14);
  --accent-border: rgba(55, 227, 164, .35);
  --accent-ink: #052e1f;
  --danger: #ff6b6b;
  --danger-soft: rgba(255, 107, 107, .12);
  --warn: #ffb020;
  --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, monospace;
  --sans: Manrope, system-ui, -apple-system, sans-serif;
  --disp: "Space Grotesk", Manrope, system-ui, sans-serif;
  --r-sm: 10px;
  --r-md: 14px;
  --r-lg: 20px;
  --shadow: 0 12px 32px rgba(0, 0, 0, .45);
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font-family: var(--sans); font-size: 15px; line-height: 1.45;
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }

::selection { background: var(--accent-soft); }
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: var(--surface-3); border-radius: 8px; }
::-webkit-scrollbar-track { background: transparent; }

button {
  font-family: var(--sans); cursor: pointer; border: none;
  background: var(--surface-2); color: var(--text);
  border-radius: var(--r-sm); padding: 9px 14px; font-size: 15px; font-weight: 600;
  transition: filter .12s, transform .06s, background .12s;
}
button:active { transform: scale(.96); }
button.primary { background: var(--accent); color: var(--accent-ink); font-weight: 800; }
button.primary.big { padding: 13px 28px; border-radius: var(--r-md); font-size: 16px; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 42px; height: 42px; padding: 0; border-radius: 13px; flex-shrink: 0;
}
.icon-btn svg { width: 21px; height: 21px; }
.icon-btn.ghost { background: transparent; color: var(--dim); }
.icon-btn.ghost:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.accent { background: var(--accent); color: var(--accent-ink); }
.icon-btn.accent:hover { filter: brightness(1.08); }
.icon-btn.danger { background: var(--danger-soft); color: var(--danger); }

input, textarea, select {
  font-family: var(--sans); font-size: 16px; /* 16px = не зумит iOS */
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--r-md); padding: 11px 14px;
  outline: none; transition: border-color .15s;
}
input:focus, textarea:focus { border-color: var(--accent-border); }
select { appearance: none; -webkit-appearance: none; }

/* ================= Login ================= */

#login-view {
  display: flex; align-items: center; justify-content: center;
  height: 100vh; height: 100dvh; padding: 24px;
  background:
    radial-gradient(600px 400px at 70% -10%, rgba(55, 227, 164, .09), transparent 65%),
    radial-gradient(500px 380px at 10% 110%, rgba(55, 227, 164, .05), transparent 60%),
    var(--bg);
}
#login-form { display: flex; flex-direction: column; gap: 14px; width: 320px; }
#login-form input { text-align: center; letter-spacing: .06em; font-family: var(--mono); font-size: 15px; padding: 13px; }
#login-form button { background: var(--accent); color: var(--accent-ink); font-weight: 800; padding: 13px; border-radius: var(--r-md); font-size: 16px; }

.login-logo {
  font-family: var(--disp); font-size: 30px; font-weight: 700; text-align: center;
  letter-spacing: .3px; display: flex; align-items: center; justify-content: center; gap: 10px;
}
.login-logo span { color: var(--accent); }
.login-logo .logo-mark { width: 30px; height: 30px; color: var(--accent); }
.login-logo.small { font-size: 17px; justify-content: flex-start; gap: 7px; }
.login-logo.small .logo-mark { width: 18px; height: 18px; }
.login-sub { text-align: center; color: var(--dim); font-size: 14px; margin: -6px 0 10px; }
.login-error { color: var(--danger); text-align: center; min-height: 20px; font-size: 13.5px; }

/* ================= Layout ================= */

#app-view { display: flex; height: 100vh; height: 100dvh; }

#sidebar {
  width: 292px; flex-shrink: 0; background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  padding-top: var(--sat);
}
.sidebar-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 14px 10px;
}
#chat-list { flex: 1; overflow-y: auto; padding: 4px 10px 10px; }
.chat-item {
  padding: 11px 13px; border-radius: var(--r-md); cursor: pointer; margin-bottom: 3px;
  transition: background .12s;
}
.chat-item:hover { background: var(--surface-2); }
.chat-item.active { background: var(--surface-2); box-shadow: inset 3px 0 0 var(--accent); }
.chat-item .ci-title { font-weight: 700; font-size: 14.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .ci-sub { font-size: 12px; color: var(--dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 3px; font-family: var(--mono); }
.chat-item .ci-busy { color: var(--accent); animation: pulse-opacity 1.6s ease-in-out infinite; }
@keyframes pulse-opacity { 0%,100% { opacity: 1 } 50% { opacity: .45 } }
.sidebar-foot {
  padding: 8px 10px calc(10px + var(--sab)) 16px; border-top: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center;
  font-size: 12px; color: var(--dim); font-family: var(--mono);
}
#sidebar-backdrop { display: none; }

#main { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }

#chat-header {
  display: flex; align-items: center; gap: 8px;
  padding: calc(8px + var(--sat)) 12px 8px; border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--surface) 82%, transparent);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  z-index: 10;
}
#btn-menu { display: none; }
#chat-title-wrap { flex: 1; min-width: 0; }
#chat-title { font-family: var(--disp); font-weight: 600; font-size: 15.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#chat-sub { font-size: 11.5px; color: var(--dim); font-family: var(--mono); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.header-actions { display: flex; gap: 2px; align-items: center; }
.header-actions select {
  padding: 7px 10px; font-size: 12.5px; border-radius: var(--r-sm);
  background: var(--surface-2); max-width: 118px; color: var(--dim);
}

/* ================= Messages ================= */

#messages {
  flex: 1; overflow-y: auto; padding: 16px 14px 10px;
  -webkit-overflow-scrolling: touch;
}
.msg { max-width: 860px; margin: 0 auto 14px; }
.msg-user { display: flex; justify-content: flex-end; }
.msg-user .bubble {
  background: linear-gradient(160deg, #17332a, #13291f);
  border: 1px solid var(--accent-border);
  border-radius: 18px 18px 5px 18px;
  padding: 10px 15px; max-width: 88%; white-space: pre-wrap; word-break: break-word;
}
.msg-user .bubble img { max-width: 230px; border-radius: 10px; display: block; margin-top: 8px; }
.attach-chip {
  display: inline-flex; align-items: center; gap: 6px; background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 8px; padding: 3px 9px;
  font-size: 12px; font-family: var(--mono); margin: 6px 6px 0 0;
}

.msg-assistant .md { line-height: 1.6; word-break: break-word; }
.md p { margin: 0 0 10px; }
.md p:last-child { margin-bottom: 0; }
.md pre {
  background: #0c1218; border: 1px solid var(--border); border-radius: var(--r-md);
  padding: 12px 14px; overflow-x: auto; font-family: var(--mono); font-size: 12.5px;
  -webkit-overflow-scrolling: touch;
}
.md code { font-family: var(--mono); font-size: .88em; background: #0c1218; padding: 1px 6px; border-radius: 6px; border: 1px solid var(--border); }
.md pre code { padding: 0; background: none; border: none; }
.md h1, .md h2, .md h3 { font-family: var(--disp); margin: 16px 0 8px; line-height: 1.3; }
.md h1 { font-size: 20px; } .md h2 { font-size: 17.5px; } .md h3 { font-size: 15.5px; }
.md table { border-collapse: collapse; margin: 8px 0; display: block; overflow-x: auto; max-width: 100%; }
.md th, .md td { border: 1px solid var(--border-strong); padding: 6px 11px; font-size: 13.5px; }
.md th { background: var(--surface-2); font-family: var(--disp); }
.md a { color: var(--accent); text-decoration-color: var(--accent-border); }
.md ul, .md ol { margin: 6px 0; padding-left: 22px; }
.md li { margin: 3px 0; }
.md blockquote { border-left: 3px solid var(--accent-border); margin: 8px 0; padding: 2px 14px; color: var(--dim); }
.md hr { border: none; border-top: 1px solid var(--border); margin: 14px 0; }

/* tool chips */
.tool-block { margin: 6px auto; max-width: 860px; }
.tool-chip {
  display: flex; align-items: center; gap: 8px; cursor: pointer;
  font-family: var(--mono); font-size: 12px; color: var(--dim);
  padding: 6px 11px; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); width: fit-content; max-width: 100%;
  transition: border-color .12s;
}
.tool-chip:hover { border-color: var(--border-strong); }
.tool-chip .t-ico { color: var(--dim); flex-shrink: 0; }
.tool-chip .t-name { color: var(--accent); font-weight: 600; flex-shrink: 0; }
.tool-chip .t-arg { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 480px; }
.tool-chip .t-err { color: var(--danger); }
.tool-detail {
  display: none; font-family: var(--mono); font-size: 11.5px; color: var(--dim);
  background: #0c1218; border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; margin-top: 4px; white-space: pre-wrap; word-break: break-word;
  max-height: 320px; overflow-y: auto;
}
.tool-block.open .tool-detail { display: block; }

.thinking-block { color: var(--dim); font-style: italic; font-size: 13px; opacity: .85; }

/* permission card */
.perm-card {
  max-width: 860px; margin: 10px auto; border: 1px solid rgba(255, 176, 32, .4);
  border-radius: var(--r-md); padding: 13px 15px; background: rgba(255, 176, 32, .06);
}
.perm-card .p-head { font-weight: 700; color: var(--warn); margin-bottom: 6px; font-size: 14px; }
.perm-card .p-body { font-family: var(--mono); font-size: 12px; white-space: pre-wrap; word-break: break-word; max-height: 200px; overflow-y: auto; color: var(--dim); }
.p-actions { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.p-actions .allow { background: var(--accent); color: var(--accent-ink); font-weight: 700; }
.p-actions .always { background: var(--surface-2); }
.p-actions .deny { background: var(--danger-soft); color: var(--danger); }
.perm-card.resolved { opacity: .55; border-color: var(--border); background: transparent; }
.perm-card.resolved .p-actions { display: none; }
.perm-status { font-size: 12.5px; margin-top: 8px; color: var(--dim); }
.perm-status:empty { display: none; }

/* ask card (AskUserQuestion) */
.ask-card {
  max-width: 860px; margin: 10px auto; border: 1px solid var(--accent-border);
  border-radius: var(--r-lg); padding: 16px; background: var(--surface);
  box-shadow: 0 6px 24px rgba(0, 0, 0, .25);
}
.ask-card .a-head { font-weight: 800; color: var(--accent); margin-bottom: 12px; font-size: 14px; font-family: var(--disp); }
.ask-card .a-question { margin-bottom: 16px; }
.ask-card .a-chip {
  display: inline-block; font-size: 10.5px; font-family: var(--mono); color: var(--accent);
  background: var(--accent-soft); border-radius: 6px; padding: 2px 8px; margin-bottom: 6px;
  text-transform: uppercase; letter-spacing: .04em;
}
.ask-card .a-text { font-weight: 700; margin-bottom: 10px; font-size: 15px; }
.ask-card .a-opts { display: flex; flex-direction: column; gap: 7px; }
.ask-card .a-opt {
  text-align: left; background: var(--surface-2); border: 1.5px solid var(--border);
  border-radius: var(--r-md); padding: 10px 14px; display: flex; flex-direction: column; gap: 2px;
  font-weight: 400;
}
.ask-card .a-opt b { font-size: 14.5px; }
.ask-card .a-opt span { font-size: 12.5px; color: var(--dim); }
.ask-card .a-opt.sel { border-color: var(--accent); background: var(--accent-soft); }
.ask-card .a-opt.sel b { color: var(--accent); }
.ask-card .a-other { width: 100%; margin-top: 8px; font-size: 14px; padding: 9px 13px; }
.ask-card.resolved .p-actions, .ask-card.resolved .a-other { display: none; }
.ask-card.resolved .a-opt { pointer-events: none; opacity: .55; }
.ask-card.resolved { border-color: var(--border); box-shadow: none; }

.result-line {
  max-width: 860px; margin: 2px auto 18px; font-size: 11.5px; color: var(--dim);
  font-family: var(--mono); text-align: right; opacity: .7;
}
.info-line { max-width: 860px; margin: 10px auto; font-size: 12.5px; color: var(--dim); text-align: center; }
.error-line {
  max-width: 860px; margin: 10px auto; font-size: 12.5px; color: var(--danger);
  font-family: var(--mono); white-space: pre-wrap; background: var(--danger-soft);
  border-radius: var(--r-sm); padding: 9px 13px;
}

.typing { display: inline-flex; gap: 5px; padding: 8px 2px; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; }
.typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%,80%,100% { opacity: .2 } 40% { opacity: 1 } }

/* ================= Welcome ================= */

#welcome {
  position: absolute; inset: 0; z-index: 5;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 10px; padding: 32px; text-align: center; background: var(--bg);
}
#welcome .w-logo { width: 56px; height: 56px; color: var(--accent); margin-bottom: 6px; }
#welcome h2 { font-family: var(--disp); margin: 0; font-size: 22px; }
#welcome p { color: var(--dim); max-width: 380px; margin: 0 0 14px; font-size: 14.5px; }

/* ================= Composer ================= */

#composer {
  padding: 8px 12px calc(10px + var(--sab));
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}
.composer-row {
  display: flex; gap: 6px; align-items: flex-end; max-width: 900px; margin: 0 auto;
  background: var(--surface-2); border: 1px solid var(--border-strong);
  border-radius: 24px; padding: 5px;
}
.composer-row:focus-within { border-color: var(--accent-border); }
#input {
  flex: 1; resize: none; max-height: 200px; line-height: 1.45;
  background: transparent; border: none; padding: 9px 6px; min-height: 24px;
}
#attach-preview { display: flex; gap: 8px; flex-wrap: wrap; max-width: 900px; margin: 0 auto; }
#attach-preview .ap-item {
  display: flex; align-items: center; gap: 7px; background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 10px; padding: 5px 10px;
  font-size: 12px; font-family: var(--mono); margin-bottom: 8px;
}
#attach-preview img { width: 34px; height: 34px; object-fit: cover; border-radius: 7px; }
#attach-preview .ap-x { cursor: pointer; color: var(--dim); padding: 2px 4px; font-size: 14px; }

/* ================= Files panel ================= */

#files-panel {
  position: fixed; top: 0; right: 0; bottom: 0; width: 400px; max-width: 100vw;
  background: var(--surface); border-left: 1px solid var(--border); z-index: 40;
  display: flex; flex-direction: column; box-shadow: var(--shadow);
  padding-top: var(--sat);
  animation: slide-in .18s ease;
}
@keyframes slide-in { from { transform: translateX(30px); opacity: 0 } to { transform: none; opacity: 1 } }
.files-head { display: flex; gap: 6px; align-items: center; padding: 10px; border-bottom: 1px solid var(--border); }
#files-path { flex: 1; font-family: var(--mono); font-size: 11.5px; color: var(--dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; direction: rtl; text-align: left; }
#files-list { flex: 1; overflow-y: auto; padding: 8px; padding-bottom: calc(8px + var(--sab)); }
.f-item {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: var(--r-sm);
  cursor: pointer; font-size: 14.5px;
}
.f-item:hover { background: var(--surface-2); }
.f-item .f-size { margin-left: auto; font-size: 11px; color: var(--dim); font-family: var(--mono); }

/* ================= Modal ================= */

#modal-backdrop {
  position: fixed; inset: 0; background: rgba(2, 6, 10, .65); z-index: 70;
  display: flex; align-items: flex-end; justify-content: center;
  backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px);
}
#new-chat-modal {
  background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: var(--r-lg) var(--r-lg) 0 0;
  padding: 20px 20px calc(20px + var(--sab)); width: 100%; max-width: 560px;
  display: flex; flex-direction: column; gap: 8px;
  animation: sheet-up .2s ease;
}
@keyframes sheet-up { from { transform: translateY(40px); opacity: .5 } to { transform: none; opacity: 1 } }
#new-chat-modal h3 { margin: 0 0 6px; font-family: var(--disp); font-size: 19px; }
#new-chat-modal label { font-size: 12px; color: var(--dim); margin-top: 8px; text-transform: uppercase; letter-spacing: .05em; font-weight: 700; }
.cwd-row { display: flex; gap: 6px; }
.cwd-row input { flex: 1; font-family: var(--mono); font-size: 13px; }
.cwd-row button { border-radius: var(--r-md); }
#nc-dirs {
  max-height: 200px; overflow-y: auto; border: 1px solid var(--border);
  border-radius: var(--r-md); font-family: var(--mono); font-size: 13px;
}
#nc-dirs .f-item { padding: 8px 12px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.modal-actions button { padding: 11px 20px; border-radius: var(--r-md); }

/* ================= Desktop ================= */

@media (min-width: 761px) {
  #modal-backdrop { align-items: center; }
  #new-chat-modal { border-radius: var(--r-lg); width: 460px; padding-bottom: 20px; }
}

/* ================= Mobile ================= */

@media (max-width: 760px) {
  #sidebar {
    position: fixed; left: 0; top: 0; bottom: 0; z-index: 60; width: min(86vw, 330px);
    transform: translateX(-102%); transition: transform .22s cubic-bezier(.2, .8, .25, 1);
    border-radius: 0 var(--r-lg) var(--r-lg) 0; border-right: none;
    box-shadow: var(--shadow);
  }
  #sidebar.open { transform: translateX(0); }
  #sidebar-backdrop.show {
    display: block; position: fixed; inset: 0; background: rgba(2, 6, 10, .55); z-index: 55;
    backdrop-filter: blur(2px); -webkit-backdrop-filter: blur(2px);
  }
  #btn-menu { display: inline-flex; }
  #chat-header { padding-left: 8px; padding-right: 8px; }
  .header-actions select { max-width: 96px; padding: 6px 7px; font-size: 11.5px; }
  #messages { padding: 12px 10px 6px; }
  .msg-user .bubble { max-width: 92%; }
  .tool-chip .t-arg { max-width: 150px; }
  .tool-detail { font-size: 11px; }
  #files-panel { width: 100vw; }
  .icon-btn { width: 40px; height: 40px; }
  .ask-card { padding: 14px; }
}

@media (display-mode: standalone) {
  body { overscroll-behavior-y: none; }
}
