:root {
  --green: #2f8f4e;
  --green-dark: #226b3a;
  --green-soft: #e8f5ec;
  --bg: #f6f7f5;
  --surface: #ffffff;
  --ink: #1c2421;
  --muted: #6b7771;
  --line: #e2e6e2;
  --user: #2f8f4e;
  --user-ink: #ffffff;
  --assistant: #ffffff;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(20,40,30,.05), 0 8px 24px rgba(20,40,30,.06);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11140f; --surface: #1a1f19; --ink: #e8ede8; --muted: #9aa79f;
    --line: #2a322a; --green-soft: #1d2a20; --assistant: #1a1f19;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
.leaf { filter: saturate(1.1); }
[hidden] { display: none !important; }

/* ---------- splash ---------- */
.splash {
  position: fixed; inset: 0; display: grid; place-items: center;
  background: var(--bg); z-index: 50;
}
.spinner {
  width: 34px; height: 34px; border-radius: 50%;
  border: 3px solid var(--line); border-top-color: var(--green);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- views ---------- */
.view { min-height: 100%; }

/* ---------- login ---------- */
#login-view {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 18px; padding: 24px;
}
.login-card {
  width: 100%; max-width: 360px; background: var(--surface);
  border: 1px solid var(--line); border-radius: 20px; box-shadow: var(--shadow);
  padding: 30px 26px; display: flex; flex-direction: column; gap: 14px;
}
.brand { font-size: 26px; font-weight: 700; letter-spacing: -.02em; }
.brand-sub { margin: -8px 0 8px; color: var(--muted); font-size: 13px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.field input {
  font: inherit; color: var(--ink); padding: 11px 13px; border-radius: 11px;
  border: 1px solid var(--line); background: var(--bg); outline: none; transition: border-color .15s;
}
.field input:focus { border-color: var(--green); }
.primary {
  margin-top: 6px; font: inherit; font-weight: 600; color: #fff; background: var(--green);
  border: none; border-radius: 11px; padding: 12px; cursor: pointer; transition: background .15s;
}
.primary:hover { background: var(--green-dark); }
.primary:disabled { opacity: .6; cursor: default; }
.error { color: #c0392b; font-size: 13px; margin: 2px 0 0; }
.foot-note { color: var(--muted); font-size: 12px; }

/* ---------- chat layout ---------- */
#chat-view { display: flex; flex-direction: column; height: 100dvh; }
.topbar {
  display: flex; align-items: center; gap: 10px; padding: 12px 16px;
  border-bottom: 1px solid var(--line); background: var(--surface);
}
.title { font-weight: 700; font-size: 17px; letter-spacing: -.01em; }
.spacer { flex: 1; }
.badge {
  font-size: 12px; color: var(--green-dark); background: var(--green-soft);
  padding: 4px 10px; border-radius: 999px; font-weight: 600;
}
.ghost {
  font: inherit; font-size: 13px; color: var(--muted); background: transparent;
  border: 1px solid var(--line); border-radius: 9px; padding: 6px 10px; cursor: pointer;
  transition: all .15s;
}
.ghost:hover { color: var(--ink); border-color: var(--muted); }
.org-select {
  font: inherit; font-size: 13px; color: var(--ink); background: var(--surface);
  border: 1px solid var(--line); border-radius: 9px; padding: 6px 10px; cursor: pointer;
  max-width: 240px; transition: border-color .15s;
}
.org-select:hover { border-color: var(--muted); }
.org-select:focus { outline: none; border-color: var(--green-dark); }
/* Подсветка select в шапке, пока организация не выбрана. */
.org-select.attention {
  border-color: var(--green); box-shadow: 0 0 0 3px var(--green-soft);
  animation: org-pulse 1.8s ease-in-out infinite;
}
@keyframes org-pulse {
  0%, 100% { box-shadow: 0 0 0 0 var(--green-soft); }
  50% { box-shadow: 0 0 0 4px var(--green-soft); }
}

/* ---------- org gate (заметная панель выбора организации над чатом) ---------- */
.org-gate {
  border-bottom: 1px solid var(--line); background: var(--green-soft);
  padding: 14px 16px; display: flex; justify-content: center;
}
.org-gate-card {
  width: 100%; max-width: 820px; display: flex; flex-wrap: wrap;
  align-items: center; gap: 6px 14px;
}
.org-gate-head { font-weight: 700; font-size: 15px; color: var(--green-dark); }
.org-gate-hint { flex: 1; min-width: 200px; font-size: 13px; color: var(--muted); }
.org-gate-select {
  font: inherit; font-size: 14px; color: var(--ink); background: var(--surface);
  border: 1px solid var(--green); border-radius: 10px; padding: 9px 12px; cursor: pointer;
  max-width: 320px; min-width: 200px; outline: none; transition: border-color .15s;
}
.org-gate-select:hover { border-color: var(--green-dark); }
.org-gate-select:focus { border-color: var(--green-dark); box-shadow: 0 0 0 3px var(--green-soft); }

/* ---------- messages ---------- */
.messages {
  flex: 1; overflow-y: auto; padding: 22px 16px; display: flex; flex-direction: column;
  gap: 14px; max-width: 820px; width: 100%; margin: 0 auto; scroll-behavior: smooth;
}
.msg { display: flex; flex-direction: column; max-width: 86%; }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.assistant { align-self: flex-start; align-items: flex-start; }
.bubble {
  padding: 11px 15px; border-radius: var(--radius); line-height: 1.5; font-size: 15px;
  word-wrap: break-word; overflow-wrap: anywhere;
}
.user .bubble {
  background: var(--user); color: var(--user-ink); border-bottom-right-radius: 5px;
}
.assistant .bubble {
  background: var(--assistant); color: var(--ink); border: 1px solid var(--line);
  border-bottom-left-radius: 5px; box-shadow: var(--shadow);
}
.bubble .err { color: #c0392b; }

/* markdown niceties */
.bubble p { margin: 0 0 .6em; } .bubble p:last-child { margin-bottom: 0; }
.bubble ul, .bubble ol { margin: .3em 0 .6em; padding-left: 1.3em; }
.bubble li { margin: .15em 0; }
.bubble code {
  background: var(--green-soft); padding: 1px 5px; border-radius: 5px; font-size: .9em;
}
.bubble pre { background: var(--green-soft); padding: 10px 12px; border-radius: 10px; overflow-x: auto; }
.bubble pre code { background: none; padding: 0; }
.bubble table { border-collapse: collapse; margin: .4em 0; font-size: 14px; }
.bubble th, .bubble td { border: 1px solid var(--line); padding: 5px 9px; text-align: left; }
.bubble a { color: var(--green-dark); }

/* ---------- status: thinking + tool chips ---------- */
.status { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted);
  background: var(--green-soft); border-radius: 999px; padding: 3px 10px; font-weight: 500;
}
.chip.done { color: var(--green-dark); }
.thinking { display: inline-flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; }
.dots { display: inline-flex; gap: 3px; }
.dots i {
  width: 5px; height: 5px; border-radius: 50%; background: var(--green);
  animation: blink 1.2s infinite both;
}
.dots i:nth-child(2) { animation-delay: .2s; }
.dots i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

/* ---------- reasoning (ход рассуждений, сворачиваемый) ---------- */
.reasoning {
  margin-bottom: 6px; border: 1px dashed var(--line); border-radius: 10px;
  background: var(--green-soft); overflow: hidden; max-width: 100%;
}
.reasoning-head {
  display: flex; align-items: center; gap: 6px; cursor: pointer; user-select: none;
  font-size: 12px; color: var(--muted); padding: 6px 11px; font-weight: 500;
}
.reasoning-head:hover { color: var(--ink); }
.r-caret { display: inline-block; transition: transform .15s; font-size: 10px; }
.reasoning.open .r-caret { transform: rotate(90deg); }
.reasoning-body {
  display: none; padding: 0 11px 9px; font-size: 13px; color: var(--muted);
  line-height: 1.5; white-space: normal; overflow-x: auto;
}
.reasoning.open .reasoning-body { display: block; padding-top: 8px; border-top: 1px solid var(--line); }
.reasoning-body:empty { display: none !important; }
.reasoning-body p:first-child { margin-top: 0; }
.reasoning-body table { font-size: 12px; }

/* шаги трейса (какие отчёты дёргались) — видны только когда панель раскрыта */
.reasoning-steps { display: none; list-style: none; margin: 0; padding: 0 11px 9px; }
.reasoning-steps:empty { display: none !important; }
.reasoning.open .reasoning-steps {
  display: flex; flex-direction: column; gap: 4px;
  padding-top: 8px; border-top: 1px solid var(--line);
}
.reasoning-step { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.reasoning-step.done { color: var(--green-dark); }

/* ---------- feedback (оценка ответа) ---------- */
.feedback { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 8px; }
.fb-label { font-size: 13px; color: var(--muted); }
.fb-rates { display: flex; flex-wrap: wrap; gap: 6px; }
.fb-rate {
  font: inherit; font-size: 13px; color: var(--muted); background: var(--surface);
  border: 1px solid var(--line); border-radius: 999px; min-width: 34px; padding: 5px 0;
  text-align: center; cursor: pointer; transition: all .15s;
}
.fb-rate:hover { border-color: var(--muted); color: var(--ink); }
.fb-rate.selected { color: var(--green-dark); border-color: var(--green); background: var(--green-soft); font-weight: 600; }
.fb-rate.low.selected { color: #c0392b; border-color: #e3a39b; background: #fbeae8; }
.fb-comment { display: flex; flex-wrap: wrap; align-items: flex-start; gap: 8px; width: 100%; margin-top: 4px; }
.fb-comment textarea {
  flex: 1; min-width: 220px; font: inherit; font-size: 14px; color: var(--ink); resize: vertical;
  padding: 8px 11px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); outline: none;
}
.fb-comment textarea:focus { border-color: var(--green); }
.fb-send {
  font: inherit; font-size: 13px; font-weight: 600; color: #fff; background: var(--green);
  border: none; border-radius: 10px; padding: 8px 14px; cursor: pointer; align-self: stretch;
}
.fb-send:hover { background: var(--green-dark); }
.fb-err { width: 100%; color: #c0392b; font-size: 12px; }
.fb-thanks { font-size: 13px; color: var(--green-dark); font-weight: 600; }

/* ---------- composer ---------- */
.composer {
  display: flex; align-items: flex-end; gap: 8px; padding: 12px 16px;
  border-top: 1px solid var(--line); background: var(--surface);
  max-width: 820px; width: 100%; margin: 0 auto;
}
#input {
  flex: 1; font: inherit; font-size: 15px; color: var(--ink); resize: none;
  max-height: 180px; padding: 11px 14px; border-radius: 14px; border: 1px solid var(--line);
  background: var(--bg); outline: none; transition: border-color .15s;
}
#input:focus { border-color: var(--green); }
.send, .stop {
  flex: none; width: 42px; height: 42px; border-radius: 50%; border: none; cursor: pointer;
  font-size: 20px; line-height: 1; color: #fff; display: grid; place-items: center;
}
.send { background: var(--green); }
.send:hover { background: var(--green-dark); }
.send:disabled { opacity: .45; cursor: default; }
.stop { background: #c0392b; font-size: 14px; }

/* ---------- secondary header actions (inline group / mobile drawer) ---------- */
.bar-actions { display: flex; align-items: center; gap: 10px; }
.icon-btn {
  font: inherit; font-size: 20px; line-height: 1; color: var(--muted);
  background: transparent; border: 1px solid var(--line); border-radius: 10px;
  width: 40px; height: 40px; display: grid; place-items: center; cursor: pointer;
  transition: all .15s;
}
.icon-btn:hover { color: var(--ink); border-color: var(--muted); }
.only-mobile { display: none; }                 /* виден только внутри @media ниже */
/* затемнение под панелью: только на мобильном (на десктопе не перекрывает страницу) */
.drawer-backdrop { position: fixed; inset: 0; background: rgba(10,20,15,.45); z-index: 40; display: none; }

/* ---------- mobile (телефоны) ---------- */
@media (max-width: 640px) {
  .only-mobile { display: grid; }
  .drawer-backdrop { display: block; }          /* затемнение активно только на мобильном */

  /* шапка с учётом «чёлки»/безопасной зоны */
  .topbar { padding: 10px 12px; padding-top: calc(10px + env(safe-area-inset-top)); gap: 8px; }
  .title  { font-size: 16px; }

  /* вторичные действия → выезжающая справа панель */
  .bar-actions {
    position: fixed; top: 0; right: 0; height: 100dvh; z-index: 41;
    width: min(82vw, 300px); flex-direction: column; align-items: stretch; gap: 12px;
    background: var(--surface); border-left: 1px solid var(--line);
    box-shadow: -8px 0 28px rgba(20,40,30,.18);
    padding: calc(16px + env(safe-area-inset-top)) 16px 16px;
    transform: translateX(100%); transition: transform .22s ease; overflow-y: auto;
  }
  .bar-actions.open { transform: translateX(0); }
  .bar-actions .badge { align-self: flex-start; }
  .bar-actions .org-select { max-width: none; width: 100%; padding: 11px 12px; }
  .bar-actions .ghost { width: 100%; text-align: left; padding: 12px 12px; font-size: 15px; }

  /* сообщения и баблы */
  .messages { padding: 16px 12px; gap: 12px; }
  .msg { max-width: 92%; }
  .bubble table { display: block; overflow-x: auto; }   /* широкие таблицы скроллятся */

  /* композер: закреплён, крупные тап-цели, выше home-bar */
  .composer { padding: 10px 12px; padding-bottom: calc(10px + env(safe-area-inset-bottom)); gap: 10px; }
  .send, .stop { width: 46px; height: 46px; }

  /* поля ввода >= 16px, чтобы iOS не зумил при фокусе */
  #input, .field input, .fb-comment textarea, .org-gate-select { font-size: 16px; }

  /* баннер выбора организации */
  .org-gate { padding: 12px; }
  .org-gate-select { max-width: none; width: 100%; }
}
