:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --border: #dfe3ea;
  --text: #1c1c1f;
  --muted: #6b7280;
  --accent: #0b8e36;
  --accent-hover: #08702b;
  --user-bg: #0b8e36;
  --user-fg: #ffffff;
  --tool-bg: #ecfdf3;
  --tool-fg: #14532d;
  --tool-border: #bbf7d0;
  --tool-result-bg: #f8fafc;
  --error-bg: #fef2f2;
  --error-fg: #991b1b;
  --error-border: #fca5a5;
  --agent-width: 420px;
  --launcher-bottom: 88px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, sans-serif;
  font-size: 15px;
  color: var(--text);
  background: #fff;
  overflow: hidden;
}

/* ─── Split layout: сайт | чат ──────────────────────────────────────────── */

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 0fr;
  height: 100vh;
  width: 100vw;
  transition: grid-template-columns 0.32s cubic-bezier(0.4, 0, 0.2, 1);
}

.layout.agent-open {
  grid-template-columns: minmax(0, 1fr) minmax(0, var(--agent-width));
}

.layout.layout-resizing {
  transition: none;
}

.layout.layout-resizing .site-pane iframe {
  pointer-events: none;
}

.site-pane {
  position: relative;
  min-width: 0;
  height: 100vh;
  overflow: hidden;
  background: #fff;
}

.site-pane iframe {
  display: block;
  width: 100%;
  height: 100%;
  border: 0;
  background: #fff;
}

.site-pane.site-pane-flash {
  box-shadow: inset 0 0 0 3px rgba(11, 142, 54, 0.45);
  transition: box-shadow 0.3s ease;
}

.agent-pane .bubble a.prodcp-link,
.tool-result a.prodcp-link {
  color: var(--accent-hover);
  text-decoration-style: dotted;
}

.agent-pane .bubble a.prodcp-link::after,
.tool-result a.prodcp-link::after {
  content: " ↗";
  font-size: 0.85em;
  opacity: 0.7;
}

.agent-pane {
  position: relative;
  min-width: 0;
  height: 100vh;
  overflow: hidden;
  background: var(--panel);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  box-shadow: -8px 0 24px rgba(15, 23, 42, 0.08);
  visibility: hidden;
  opacity: 0;
  transition: opacity 0.24s ease, visibility 0.24s ease;
}

.layout.agent-open .agent-pane {
  visibility: visible;
  opacity: 1;
}

/* ─── Resize handle ─────────────────────────────────────────────────────── */

.resize-handle {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 10px;
  margin-left: -5px;
  cursor: col-resize;
  z-index: 30;
  touch-action: none;
  user-select: none;
}

.resize-handle::before {
  content: "";
  position: absolute;
  inset: 0;
}

.resize-handle::after {
  content: "";
  position: absolute;
  left: 4px;
  top: 50%;
  transform: translateY(-50%);
  width: 3px;
  height: 56px;
  border-radius: 3px;
  background: var(--border);
  transition: background 0.15s ease, height 0.15s ease;
}

.resize-handle:hover::after,
.layout-resizing .resize-handle::after {
  background: var(--accent);
  height: 72px;
}

.resize-handle:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

/* ─── Launcher (кнопка на сайте, не на чате) ───────────────────────────── */

.chat-launcher {
  position: absolute;
  right: 24px;
  bottom: var(--launcher-bottom);
  z-index: 50;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  height: 52px;
  padding: 0 20px 0 16px;
  border: none;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  box-shadow:
    0 8px 24px rgba(11, 142, 54, 0.35),
    0 2px 8px rgba(0, 0, 0, 0.12);
  transition: transform 0.15s ease, background 0.15s ease, opacity 0.2s ease;
}

.chat-launcher:hover {
  background: var(--accent-hover);
  transform: translateY(-2px);
}

.layout.agent-open .chat-launcher {
  opacity: 0;
  pointer-events: none;
  transform: translateY(8px);
}

.launcher-icon {
  flex-shrink: 0;
}

/* ─── Agent header & toolbar ────────────────────────────────────────────── */

.agent-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 16px 12px;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(180deg, #ecfdf3 0%, #fff 70%);
}

.agent-header-text h2 {
  margin: 0;
  font-size: 17px;
  line-height: 1.25;
}

.agent-header-text p {
  margin: 6px 0 0;
  font-size: 12px;
  line-height: 1.4;
  color: var(--muted);
}

.agent-close {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  background: #fff;
  border: 1px solid var(--border);
  color: var(--muted);
}

.agent-close:hover {
  background: var(--bg);
  color: var(--text);
}

.agent-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}

.agent-toolbar .compact {
  padding: 7px 12px;
  font-size: 13px;
}

.history-toggle {
  margin-left: auto;
  padding: 7px 12px;
  font-size: 13px;
  background: var(--bg);
  border: 1px solid var(--border);
}

.history-toggle[aria-expanded="true"] {
  background: #dcfce7;
  border-color: #86efac;
  color: var(--accent-hover);
}

.history-drawer {
  max-height: 180px;
  overflow-y: auto;
  border-bottom: 1px solid var(--border);
  background: #fafbfc;
}

.conv-list {
  list-style: none;
  margin: 0;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.conv-list li {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid transparent;
  background: #fff;
  font-size: 13px;
  cursor: pointer;
}

.conv-list li:hover {
  border-color: var(--border);
}

.conv-list li.active {
  background: #dcfce7;
  border-color: #86efac;
  color: var(--accent-hover);
}

.conv-list .conv-title {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-list .conv-del {
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 0 4px;
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  opacity: 0.5;
}

.conv-list li:hover .conv-del {
  opacity: 1;
}

/* ─── Buttons ───────────────────────────────────────────────────────────── */

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  font-size: 14px;
  padding: 8px 12px;
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
}

button.primary {
  background: var(--accent);
  color: white;
}

button.primary:hover {
  background: var(--accent-hover);
}

button.primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

button.stop {
  background: #fff;
  border: 1px solid var(--border);
  color: var(--text);
}

button.stop:hover {
  background: #fee2e2;
  color: var(--error-fg);
  border-color: var(--error-border);
}

/* ─── Chat ──────────────────────────────────────────────────────────────── */

.chat {
  flex: 1;
  overflow-y: auto;
  padding: 14px 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: var(--bg);
}

.msg {
  width: 100%;
}

.msg.user .bubble {
  background: var(--user-bg);
  color: var(--user-fg);
  border-radius: 16px 16px 4px 16px;
  margin-left: auto;
  max-width: 90%;
  width: fit-content;
  padding: 10px 14px;
  white-space: pre-wrap;
  word-wrap: break-word;
  font-size: 14px;
  line-height: 1.45;
}

.msg.assistant .bubble {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px 16px 16px 4px;
  padding: 12px 14px;
  word-wrap: break-word;
  line-height: 1.55;
  font-size: 14px;
}

.msg.assistant .bubble:empty {
  display: none;
}

.msg .bubble h1,
.msg .bubble h2,
.msg .bubble h3,
.msg .bubble h4 {
  font-size: 1.05em;
  font-weight: 600;
  margin: 10px 0 6px;
}

.msg .bubble p {
  margin: 0 0 8px;
}

.msg .bubble p:last-child {
  margin-bottom: 0;
}

.msg .bubble ul,
.msg .bubble ol {
  margin: 6px 0 8px;
  padding-left: 20px;
}

.msg .bubble a {
  color: var(--accent-hover);
  text-decoration: underline;
}

.msg .bubble code {
  background: rgba(0, 0, 0, 0.05);
  padding: 1px 5px;
  border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.92em;
}

.msg .bubble pre {
  background: #f3f4f6;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  overflow-x: auto;
  font-size: 12px;
}

.msg .bubble .md-table-wrap {
  overflow-x: auto;
  margin: 8px 0 12px;
  -webkit-overflow-scrolling: touch;
}

.msg .bubble table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  line-height: 1.4;
}

.msg .bubble th,
.msg .bubble td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
  vertical-align: top;
}

.msg .bubble th {
  background: #f3f4f6;
  font-weight: 600;
}

.msg .bubble tr:nth-child(even) td {
  background: #fafafa;
}

.msg .bubble blockquote {
  margin: 8px 0;
  padding: 8px 12px;
  border-left: 3px solid var(--accent);
  background: #f9fafb;
  color: var(--muted);
}

.msg .bubble blockquote p:last-child {
  margin-bottom: 0;
}

.msg .bubble hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 12px 0;
}

.msg .bubble del {
  color: var(--muted);
}

.msg .bubble img {
  max-width: 100%;
  height: auto;
  border-radius: 6px;
}

.tool-result .md-table-wrap,
.tool-result table {
  font-size: 12px;
}

.thinking {
  background: #fef9e7;
  border: 1px solid #fde68a;
  border-radius: 8px;
  margin-bottom: 8px;
  font-size: 12px;
}

.agent-steps {
  margin-bottom: 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #f8fafc;
  font-size: 12.5px;
  overflow: hidden;
}

.agent-steps > summary {
  padding: 8px 12px;
  cursor: pointer;
  user-select: none;
  font-weight: 600;
  color: var(--muted);
  list-style: none;
}

.agent-steps > summary::-webkit-details-marker {
  display: none;
}

.agent-steps > summary::before {
  content: "▸ ";
  display: inline-block;
  transition: transform 0.15s ease;
}

.agent-steps[open] > summary::before {
  transform: rotate(90deg);
}

.agent-steps-body {
  padding: 0 10px 10px;
  border-top: 1px solid var(--border);
}

.agent-steps:not([open]) > summary {
  border-bottom: none;
  background: #f0fdf4;
  color: var(--accent-hover);
}

.agent-steps:not([open]) {
  border-color: #bbf7d0;
}

.agent-steps[open] > summary {
  border-bottom: 1px solid var(--border);
  margin-bottom: 8px;
}

.thinking > summary {
  padding: 6px 10px;
  cursor: pointer;
  color: #854d0e;
}

.thinking-text {
  margin: 0;
  padding: 8px 10px 10px;
  border-top: 1px solid #fde68a;
  font-family: ui-monospace, monospace;
  font-size: 11px;
  white-space: pre-wrap;
  max-height: 200px;
  overflow-y: auto;
}

.tools-row {
  margin-bottom: 6px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.tool-chip {
  background: var(--tool-bg);
  border: 1px solid var(--tool-border);
  border-radius: 10px;
  font-size: 12px;
  overflow: hidden;
}

.tool-chip > summary {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  cursor: pointer;
  list-style: none;
  color: var(--tool-fg);
}

.tool-chip > summary::-webkit-details-marker {
  display: none;
}

.tool-chip .tool-spinner {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--tool-fg);
  opacity: 0.4;
}

.tool-chip.running .tool-spinner {
  animation: pulse 1s infinite ease-in-out;
}

@keyframes pulse {
  0%, 100% { opacity: 0.3; transform: scale(0.85); }
  50% { opacity: 0.9; transform: scale(1.1); }
}

.tool-body {
  padding: 8px 10px;
  border-top: 1px solid var(--tool-border);
  background: var(--tool-result-bg);
}

.tool-args {
  background: #1c1c1f;
  color: #e5e7eb;
  padding: 6px 8px;
  border-radius: 6px;
  font-size: 11px;
  font-family: ui-monospace, monospace;
  overflow-x: auto;
  max-height: 140px;
  overflow-y: auto;
}

.tool-result {
  font-size: 12px;
  max-height: 240px;
  overflow-y: auto;
}

.progress-line {
  font-size: 11px;
  color: var(--muted);
  font-style: italic;
}

/* ─── Composer ──────────────────────────────────────────────────────────── */

.composer {
  padding: 10px 12px 12px;
  border-top: 1px solid var(--border);
  background: var(--panel);
  flex-shrink: 0;
}

.composer-box {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 5px 6px 5px 12px;
  border: 1.5px solid var(--border);
  border-radius: 16px;
  background: #fff;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.composer-box:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(11, 142, 54, 0.14);
}

.composer.composer-busy .composer-box {
  border-color: #fcd34d;
  box-shadow: 0 0 0 3px rgba(252, 211, 77, 0.25);
}

.composer textarea {
  flex: 1;
  min-width: 0;
  resize: none;
  height: var(--composer-line-height, 20px);
  min-height: var(--composer-line-height, 20px);
  max-height: 140px;
  padding: 0;
  margin: 5px 0;
  border: none;
  background: transparent;
  font-family: inherit;
  font-size: 14px;
  line-height: 1.45;
  color: var(--text);
  overflow-y: hidden;
  box-sizing: border-box;
}

.composer textarea:focus {
  outline: none;
}

.composer textarea::placeholder {
  color: #9ca3af;
}

.composer textarea:disabled {
  color: var(--muted);
  cursor: not-allowed;
}

.composer-tools {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.composer .icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  padding: 0;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  transition: background 0.15s ease, color 0.15s ease, transform 0.1s ease;
}

.composer .icon-btn:hover:not(:disabled) {
  background: var(--bg);
  color: var(--text);
}

.composer .icon-btn:active:not(:disabled) {
  transform: scale(0.94);
}

.composer .mic.recording {
  color: var(--error-fg);
  background: #fee2e2;
  animation: pulse-bg 1s infinite;
}

.composer .send-btn {
  background: var(--accent);
  color: #fff;
}

.composer .send-btn:hover:not(:disabled) {
  background: var(--accent-hover);
  color: #fff;
}

.composer .send-btn:disabled {
  background: #e5e7eb;
  color: #9ca3af;
  cursor: not-allowed;
  opacity: 1;
}

.composer.composer-busy .send-btn {
  display: none !important;
}

.composer .stop-btn {
  display: none !important;
  background: #fef3c7;
  color: #b45309;
}

.composer.composer-busy .stop-btn {
  display: inline-flex !important;
}

.composer .stop-btn:hover {
  background: #fde68a;
  color: #92400e;
}

.composer-hint {
  margin: 6px 4px 0;
  font-size: 11px;
  line-height: 1.3;
  color: #9ca3af;
  text-align: center;
}

.composer.composer-busy .composer-hint {
  color: #b45309;
}

.error {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--error-fg);
  background: var(--error-bg);
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--error-border);
  font-size: 13px;
}

.retry-btn {
  background: #fff;
  border: 1px solid var(--error-border);
  color: var(--error-fg);
  padding: 4px 10px;
  font-size: 12px;
}

/* ─── Mobile: чат поверх сайта ──────────────────────────────────────────── */

@media (max-width: 900px) {
  :root {
    --agent-width: 100vw;
    --launcher-bottom: 96px;
  }

  .layout.agent-open {
    grid-template-columns: 0fr minmax(0, 1fr);
  }

  .layout.agent-open .site-pane {
    visibility: hidden;
  }

  .resize-handle {
    display: none;
  }
}

@media (max-width: 520px) {
  .chat-launcher .launcher-label {
    display: none;
  }

  .chat-launcher {
    width: 52px;
    height: 52px;
    padding: 0;
    justify-content: center;
  }
}
