:root {
  --bg: var(--tg-theme-bg-color, #0e1014);
  --fg: var(--tg-theme-text-color, #eef0f4);
  --muted: var(--tg-theme-hint-color, #8a919c);
  --accent: var(--tg-theme-button-color, #3d8bfd);
  --accent-fg: var(--tg-theme-button-text-color, #fff);
  --surface: color-mix(in srgb, var(--fg) 6%, var(--bg));
  --surface-2: color-mix(in srgb, var(--fg) 10%, var(--bg));
  --border: color-mix(in srgb, var(--fg) 11%, transparent);
  --user-bg: color-mix(in srgb, var(--accent) 26%, var(--bg));
  --sys: #e0a84a;
  --danger: #e35d6a;
  --ok: #6fcea0;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --font: "Segoe UI", ui-sans-serif, system-ui, -apple-system, sans-serif;
  --mono: ui-monospace, "Cascadia Code", "SF Mono", Menlo, Consolas, monospace;
  --radius: 12px;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  -webkit-tap-highlight-color: transparent;
}
body {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.top {
  padding: 14px 16px 10px;
  background: linear-gradient(
    180deg,
    color-mix(in srgb, var(--accent) 10%, var(--bg)) 0%,
    var(--bg) 100%
  );
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.brand-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.brand-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
.brand {
  font-weight: 700;
  letter-spacing: -0.02em;
  font-size: 18px;
}
.changes-wrap {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 2px;
  flex-shrink: 0;
  max-width: 46%;
}
.changes-btn {
  flex-shrink: 0;
  font-size: 11px;
  padding: 7px 10px;
  color: var(--muted);
}
.changes-hint {
  font-size: 10px;
  line-height: 1.2;
  color: var(--muted);
  padding: 0 2px;
  max-width: 100%;
  text-align: right;
}
.badge {
  font-size: 11px;
  letter-spacing: 0.04em;
  padding: 3px 9px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--muted);
  background: var(--surface);
  text-transform: none;
}
.badge.agent {
  color: var(--ok);
  border-color: color-mix(in srgb, var(--ok) 45%, transparent);
  background: color-mix(in srgb, var(--ok) 12%, var(--bg));
}
.badge.ask {
  color: #9ec0ff;
  border-color: color-mix(in srgb, #9ec0ff 40%, transparent);
  background: color-mix(in srgb, #9ec0ff 12%, var(--bg));
}
.badge.plan {
  color: #ebc77a;
  border-color: color-mix(in srgb, #ebc77a 40%, transparent);
  background: color-mix(in srgb, #ebc77a 12%, var(--bg));
}
.sub {
  margin-top: 6px;
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.slots {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  padding: 10px 16px 0;
  flex-shrink: 0;
}
.slot-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  border-radius: var(--radius);
  padding: 9px 8px;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background .15s, color .15s, border-color .15s;
}
.slot-btn.active {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: transparent;
}
.slot-btn.busy {
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--sys) 55%, transparent);
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 16px;
  flex-shrink: 0;
}
.seg {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--surface);
}
.seg-btn {
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 8px 11px;
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}
.seg-btn.active {
  background: var(--accent);
  color: var(--accent-fg);
}
.toolbar-actions { display: flex; gap: 6px; }
.icon-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--fg);
  border-radius: var(--radius);
  padding: 8px 11px;
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}
.icon-btn.danger {
  color: var(--danger);
  border-color: color-mix(in srgb, var(--danger) 35%, var(--border));
}
.icon-btn:disabled {
  opacity: 0.4;
  cursor: default;
}

.chat {
  flex: 1;
  overflow-y: auto;
  padding: 4px 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  -webkit-overflow-scrolling: touch;
}
.empty {
  margin: auto;
  text-align: center;
  color: var(--muted);
  max-width: 300px;
  padding: 28px 10px;
}
.empty strong {
  display: block;
  color: var(--fg);
  margin-bottom: 8px;
  font-size: 17px;
  letter-spacing: -0.02em;
}
.empty p {
  margin: 0;
  font-size: 13px;
  line-height: 1.5;
}

.msg {
  max-width: 92%;
  padding: 11px 13px;
  border-radius: 16px;
  white-space: pre-wrap;
  word-break: break-word;
  border: 1px solid var(--border);
  background: var(--surface);
}
.msg.user {
  align-self: flex-end;
  background: var(--user-bg);
  border-color: color-mix(in srgb, var(--accent) 30%, transparent);
  border-bottom-right-radius: 6px;
}
.msg.assistant {
  align-self: flex-start;
  border-bottom-left-radius: 6px;
}
.msg.system {
  align-self: center;
  color: var(--sys);
  font-size: 13px;
  background: transparent;
  border-style: dashed;
  max-width: 95%;
}
.msg .meta {
  display: block;
  font-size: 11px;
  color: var(--muted);
  margin-bottom: 5px;
  font-weight: 600;
}
.msg .imgs {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 8px;
}
.msg .imgs img,
.msg img.chat-img {
  max-width: 100%;
  border-radius: 10px;
  display: block;
  margin-top: 8px;
  background: color-mix(in srgb, var(--fg) 6%, transparent);
  min-height: 48px;
}
.msg .imgs img {
  margin-top: 0;
}
.msg img.chat-img[src] {
  min-height: 0;
  background: transparent;
}
.msg.streaming::after {
  content: "▍";
  animation: blink 1s steps(1) infinite;
  margin-left: 2px;
  color: var(--muted);
}
@keyframes blink {
  50% { opacity: 0; }
}
.msg code, .msg pre {
  font-family: var(--mono);
  font-size: 12.5px;
}
.msg pre {
  margin: 8px 0 0;
  padding: 10px;
  border-radius: 10px;
  background: var(--surface-2);
  overflow-x: auto;
}

.composer-wrap {
  flex-shrink: 0;
  padding: 10px 14px calc(14px + var(--safe-bottom));
  border-top: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg) 94%, #000);
}
.attach-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 0 4px 10px;
}
.attach-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 100%;
  padding: 5px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 12px;
  color: var(--fg);
}
.attach-chip span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 160px;
}
.attach-chip button {
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  padding: 0 2px;
}

/* Cursor-like unified input bar */
.composer-shell {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 8px 8px 8px;
  border-radius: 22px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.22);
}
.plus-btn {
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 50%;
  background: color-mix(in srgb, var(--fg) 10%, transparent);
  color: var(--fg);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
}
.plus-btn:disabled {
  opacity: 0.4;
  cursor: default;
}
.plus-btn:active:not(:disabled) {
  background: color-mix(in srgb, var(--fg) 16%, transparent);
}
.composer-shell textarea {
  flex: 1;
  resize: none;
  max-height: 120px;
  min-height: 36px;
  border: 0;
  background: transparent;
  color: var(--fg);
  padding: 8px 2px;
  font: inherit;
  font-size: 15px;
  line-height: 1.35;
  outline: none;
}
.composer-shell textarea::placeholder {
  color: color-mix(in srgb, var(--muted) 85%, transparent);
}
.composer-tools {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
  padding-bottom: 1px;
}
.model-chip {
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  padding: 6px 8px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--fg) 6%, transparent);
  max-width: 88px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.action-btn {
  width: 34px;
  height: 34px;
  border: 0;
  border-radius: 50%;
  background: var(--fg);
  color: var(--bg);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
}
.action-btn .ico-stop { display: none; }
.action-btn.stop-mode {
  background: color-mix(in srgb, var(--fg) 18%, transparent);
  color: var(--fg);
}
.action-btn.stop-mode .ico-send { display: none; }
.action-btn.stop-mode .ico-stop { display: block; }
.action-btn:disabled {
  opacity: 0.35;
  cursor: default;
}

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(88px + var(--safe-bottom));
  transform: translateX(-50%);
  background: #1a1e27;
  color: #fff;
  padding: 9px 14px;
  border-radius: 12px;
  font-size: 13px;
  z-index: 20;
  max-width: 90%;
  border: 1px solid var(--border);
}

.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 30;
}
.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: min(88vh, 720px);
  display: flex;
  flex-direction: column;
  background: var(--bg);
  border-top: 1px solid var(--border);
  border-radius: 16px 16px 0 0;
  z-index: 31;
  box-shadow: 0 -12px 40px rgba(0, 0, 0, 0.35);
  padding-bottom: var(--safe-bottom);
}
/* display:flex выше перебивает HTML [hidden] — без этого панель всегда видна и жрёт композер */
.sheet[hidden],
.sheet-backdrop[hidden] {
  display: none !important;
}
.sheet-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px 10px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.sheet-title {
  font-weight: 700;
  font-size: 16px;
  letter-spacing: -0.02em;
}
.sheet-sub {
  margin-top: 4px;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.35;
}
.sheet-body {
  overflow-y: auto;
  padding: 12px 16px 20px;
  -webkit-overflow-scrolling: touch;
}
.chg-section {
  margin-bottom: 18px;
}
.chg-section:last-child {
  margin-bottom: 4px;
}
.chg-h {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 0 0 10px;
}
.chg-topic {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: var(--radius);
  padding: 10px 12px;
  margin-bottom: 8px;
}
.chg-topic-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}
.chg-topic-name {
  font-weight: 650;
  font-size: 14px;
}
.chg-pill {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--muted);
  white-space: nowrap;
}
.chg-pill.dirty {
  color: var(--sys);
  border-color: color-mix(in srgb, var(--sys) 45%, transparent);
  background: color-mix(in srgb, var(--sys) 12%, var(--bg));
}
.chg-pill.ok {
  color: var(--ok);
  border-color: color-mix(in srgb, var(--ok) 40%, transparent);
  background: color-mix(in srgb, var(--ok) 12%, var(--bg));
}
.chg-pill.both {
  color: #9ec0ff;
  border-color: color-mix(in srgb, #9ec0ff 40%, transparent);
  background: color-mix(in srgb, #9ec0ff 12%, var(--bg));
}
.chg-list {
  margin: 0;
  padding: 0;
  list-style: none;
}
.chg-list li {
  font-size: 12.5px;
  color: var(--muted);
  padding: 3px 0;
  display: flex;
  gap: 8px;
  align-items: baseline;
  word-break: break-word;
}
.chg-list .path,
.chg-list .subj {
  color: var(--fg);
  flex: 1;
  min-width: 0;
}
.chg-list .mark {
  flex-shrink: 0;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--muted);
  min-width: 3.2em;
}
.chg-empty {
  font-size: 13px;
  color: var(--muted);
  padding: 4px 2px 8px;
}
.chg-hint {
  font-size: 12px;
  color: var(--muted);
  margin: 0 0 12px;
  line-height: 1.4;
}
