:root {
  --bg0: #05070b;
  --bg1: #0a1018;
  --bg-side: #080b10;
  --bg-panel: #0c1118;
  --line: rgba(0, 229, 255, 0.18);
  --line-strong: rgba(0, 229, 255, 0.42);
  --cyan: #00e5ff;
  --cyan-soft: rgba(0, 229, 255, 0.12);
  --text: #e8eef6;
  --muted: #8a97a8;
  --metal: #c9d4e0;
  --danger: #ff5d7a;
  --ok: #3dffb5;
  --user-bg: rgba(0, 229, 255, 0.08);
  --code-bg: #0a121c;
  --radius: 16px;
  --font-ui: "IBM Plex Sans", "Segoe UI", system-ui, sans-serif;
  --font-mono: "JetBrains Mono", "Cascadia Code", ui-monospace, monospace;
  --side-w: 260px;
  --shadow: 0 0 0 1px rgba(0, 229, 255, 0.08), 0 18px 50px rgba(0, 0, 0, 0.45);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  height: 100dvh;
  background: var(--bg0);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 15px;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}
body {
  background:
    radial-gradient(900px 480px at 12% -8%, rgba(0, 229, 255, 0.08), transparent 55%),
    radial-gradient(700px 420px at 100% 0%, rgba(61, 255, 181, 0.05), transparent 50%),
    var(--bg0);
  overflow: hidden;
  touch-action: manipulation;
}

.app {
  height: 100%;
  height: 100dvh;
  display: grid;
  grid-template-columns: var(--side-w) 1fr;
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}

.backdrop {
  position: fixed;
  inset: 0;
  z-index: 30;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(2px);
  border: 0;
  padding: 0;
  margin: 0;
  cursor: pointer;
}
.backdrop[hidden] { display: none !important; }

/* —— Sidebar —— */
.sidebar {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: calc(14px + var(--safe-top)) 12px 14px;
  background: color-mix(in srgb, var(--bg-side) 92%, #000);
  border-right: 1px solid var(--line);
  min-height: 0;
}
.side-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 6px 10px;
}
.side-brand > div:nth-child(2) { flex: 1; min-width: 0; }
.logo {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  font-weight: 700;
  letter-spacing: 0.02em;
  color: #041018;
  background: linear-gradient(145deg, var(--cyan), #3dffb5);
  box-shadow: 0 0 24px rgba(0, 229, 255, 0.25);
  flex-shrink: 0;
}
.logo-round,
img.logo {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  object-fit: cover;
  display: block;
  border: 1px solid var(--line-strong);
  box-shadow: 0 0 0 2px rgba(0, 229, 255, 0.12), 0 0 22px rgba(0, 229, 255, 0.28);
  background: #0a1018;
  flex-shrink: 0;
}
.top-logo {
  width: 28px;
  height: 28px;
  margin-right: 2px;
}
.brand-name {
  font-weight: 700;
  letter-spacing: 0.08em;
  font-size: 1rem;
}
.brand-sub {
  color: var(--muted);
  font-size: 0.75rem;
}
.side-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--text);
  border-radius: 12px;
  padding: 12px 14px;
  min-height: 44px;
  cursor: pointer;
  font: inherit;
  text-align: left;
  transition: border-color 0.15s, background 0.15s;
}
.side-btn:hover { border-color: var(--line-strong); background: var(--cyan-soft); }
.side-btn.primary {
  background: color-mix(in srgb, var(--cyan-soft) 80%, transparent);
  border-color: var(--line-strong);
}
.side-btn .ico { color: var(--cyan); font-size: 1.05rem; }
.side-section {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.side-label {
  color: var(--muted);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 4px 6px;
}
.history-list {
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding-right: 2px;
  -webkit-overflow-scrolling: touch;
}
.hist-item {
  border: 0;
  background: transparent;
  color: var(--metal);
  text-align: left;
  padding: 12px 12px;
  min-height: 44px;
  border-radius: 10px;
  cursor: pointer;
  font: inherit;
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.hist-row {
  display: flex;
  align-items: stretch;
  gap: 2px;
  border-radius: 10px;
}
.hist-row .hist-item {
  flex: 1;
  min-width: 0;
}
.hist-row.active,
.hist-row:hover {
  background: var(--cyan-soft);
}
.hist-row.active .hist-item,
.hist-row:hover .hist-item {
  color: var(--text);
}
.hist-item:hover {
  background: transparent;
  color: var(--text);
}
.hist-del {
  border: 0;
  background: transparent;
  color: var(--muted);
  width: 36px;
  min-width: 36px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 1.15rem;
  line-height: 1;
}
.hist-del:hover {
  color: var(--danger);
  background: rgba(255, 93, 122, 0.12);
}
.side-foot {
  border-top: 1px solid var(--line);
  padding-top: 10px;
  padding-bottom: var(--safe-bottom);
  font-size: 0.75rem;
  color: var(--muted);
}
.foot-mono {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  color: #3aa9c0;
  margin-top: 4px;
  word-break: break-all;
}

/* —— Main —— */
.main {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100%;
  height: 100dvh;
}
.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(12px + var(--safe-top)) 22px 12px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--bg1) 70%, transparent);
  backdrop-filter: blur(8px);
}
.top-title {
  font-weight: 600;
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.top-meta {
  color: var(--muted);
  font-family: var(--font-mono);
  font-size: 0.75rem;
}
.icon-btn {
  border: 1px solid var(--line);
  background: transparent;
  color: var(--text);
  border-radius: 10px;
  width: 40px;
  height: 40px;
  min-width: 40px;
  cursor: pointer;
  display: inline-grid;
  place-items: center;
  font-size: 1.05rem;
}
.mobile-only { display: none; }
.mobile-hint { display: none; }
.desktop-hint { display: block; }

.messages {
  overflow: auto;
  padding: 22px 0 12px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.turn {
  max-width: 760px;
  margin: 0 auto;
  padding: 0 22px 18px;
}
.turn.user .bubble {
  margin-left: auto;
  max-width: min(640px, 92%);
  background: var(--user-bg);
  border: 1px solid var(--line);
  border-radius: 16px 16px 4px 16px;
  padding: 12px 14px;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.5;
}
.turn.bot .bubble {
  max-width: 760px;
  padding: 4px 2px;
  line-height: 1.55;
  color: var(--metal);
  word-break: break-word;
}
.turn.bot .bubble.err { color: #ffd0d5; }
.turn.notice .bubble {
  color: var(--muted, #9aa3b2);
  opacity: 0.95;
}
.turn.notice .msg-actions {
  display: none !important;
}
.msg-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  padding: 8px 0 2px;
  border-top: 1px solid var(--line);
}
.act-btn {
  border: 1px solid var(--line-strong);
  background: var(--cyan-soft);
  color: var(--text);
  border-radius: 10px;
  padding: 7px 12px;
  min-height: 34px;
  font: inherit;
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
  pointer-events: auto;
  -webkit-tap-highlight-color: transparent;
}
.act-btn:hover {
  color: var(--cyan);
  border-color: var(--cyan);
  background: rgba(0, 229, 255, 0.18);
}
.act-btn:disabled,
.act-btn[disabled] {
  opacity: 0.45;
  cursor: not-allowed;
}
.act-note {
  color: var(--ok);
  font-size: 0.75rem;
  margin-left: 4px;
}
.code-wrap {
  position: relative;
  margin: 0.6em 0 0.9em;
}
.code-wrap pre {
  margin: 0;
}
.code-copy {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 2;
  border: 1px solid var(--line-strong);
  background: rgba(8, 14, 22, 0.92);
  color: var(--cyan);
  border-radius: 8px;
  padding: 4px 10px;
  font: inherit;
  font-size: 0.72rem;
  font-weight: 600;
  cursor: pointer;
}
.code-copy:hover {
  background: var(--cyan-soft);
}
.role-tag {
  font-size: 0.7rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--cyan);
  margin-bottom: 6px;
  font-weight: 600;
}
.turn.user .role-tag { color: var(--muted); text-align: right; }

.md p { margin: 0 0 0.7em; }
.md p:last-child { margin-bottom: 0; }
.md ul, .md ol { margin: 0.4em 0 0.8em; padding-left: 1.3em; }
.md li { margin: 0.2em 0; }
.md a { color: var(--cyan); }
.md code {
  font-family: var(--font-mono);
  font-size: 0.88em;
  background: rgba(0, 229, 255, 0.08);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.1em 0.35em;
}
.md pre {
  margin: 0.75em 0;
  background: var(--code-bg);
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: auto;
  box-shadow: var(--shadow);
  -webkit-overflow-scrolling: touch;
}
.md pre code {
  display: block;
  background: transparent;
  border: 0;
  padding: 12px 14px;
  font-size: 0.84rem;
  line-height: 1.45;
  color: #d7e6f5;
  white-space: pre;
}
.md strong { color: var(--text); font-weight: 600; }
.md .file-card {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin: 6px 0;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--cyan-soft);
  color: var(--cyan);
  text-decoration: none;
  font-family: var(--font-mono);
  font-size: 0.85rem;
  word-break: break-all;
}
.md .file-card.img {
  display: block;
  padding: 8px;
  max-width: min(420px, 100%);
}
.md .file-card img {
  display: block;
  max-width: 100%;
  border-radius: 8px;
}

.typing::after { content: none; }
.bubble.thinking {
  padding: 10px 4px !important;
}
.think-pulse {
  display: flex;
  align-items: center;
  gap: 14px;
  min-height: 44px;
}
.think-orbit {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  position: relative;
  background:
    conic-gradient(from 120deg, transparent, rgba(0, 229, 255, 0.85), transparent 70%);
  animation: think-spin 1.6s linear infinite;
  flex-shrink: 0;
}
.think-orbit::after {
  content: "";
  position: absolute;
  inset: 3px;
  border-radius: 50%;
  background: var(--bg1);
}
.think-core {
  position: relative;
  z-index: 1;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--cyan);
  box-shadow: 0 0 14px rgba(0, 229, 255, 0.7);
  animation: think-pulse 1.1s ease-in-out infinite;
}
.think-copy {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}
.think-kicker {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  letter-spacing: 0.14em;
  color: var(--cyan);
}
.think-line {
  color: var(--metal);
  font-size: 0.92rem;
  animation: think-fade 1.4s ease-in-out infinite;
}
@keyframes think-spin { to { transform: rotate(360deg); } }
@keyframes think-pulse {
  0%, 100% { transform: scale(0.85); opacity: 0.7; }
  50% { transform: scale(1.15); opacity: 1; }
}
@keyframes think-fade {
  0%, 100% { opacity: 0.55; }
  50% { opacity: 1; }
}
.md pre code.hljs {
  background: transparent;
  padding: 12px 14px;
}

.quick-chips {
  display: flex;
  flex-wrap: nowrap;
  gap: 6px;
  overflow-x: auto;
  max-width: 760px;
  margin: 0 auto;
  padding: 0 2px 8px;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.quick-chips::-webkit-scrollbar { display: none; }
.qchip {
  flex: 0 0 auto;
  border: 1px solid var(--line);
  background: var(--cyan-soft);
  color: var(--text);
  border-radius: 999px;
  padding: 6px 10px;
  font: inherit;
  font-size: 0.78rem;
  cursor: pointer;
  white-space: nowrap;
}
.qchip:active { border-color: var(--line-strong); }

.agent-select {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--bg-panel);
  color: var(--text);
  border-radius: 10px;
  padding: 8px 10px;
  font: inherit;
  font-size: 0.85rem;
}
.agent-rail {
  display: flex;
  align-items: center;
  gap: 8px;
  max-width: 760px;
  margin: 0 auto 8px;
}
.agent-rail-label {
  font-size: 0.75rem;
  color: var(--muted);
  white-space: nowrap;
}
.agent-rail .agent-select {
  flex: 1;
}
.fab-scroll {
  position: fixed;
  right: calc(18px + var(--safe-right));
  bottom: calc(96px + var(--safe-bottom));
  z-index: 20;
  width: 40px;
  height: 40px;
  border-radius: 999px;
  border: 1px solid var(--line-strong);
  background: color-mix(in srgb, var(--bg-panel) 90%, #000);
  color: var(--cyan);
  font: inherit;
  font-size: 1.1rem;
  cursor: pointer;
  box-shadow: var(--shadow);
}
.fab-scroll[hidden] { display: none !important; }
.auth-only[hidden] { display: none !important; }

.preset-rail {
  display: flex;
  flex-wrap: nowrap;
  gap: 6px;
  overflow-x: auto;
  max-width: 760px;
  margin: 0 auto 8px;
  padding: 0 2px;
  scrollbar-width: none;
}
.preset-rail::-webkit-scrollbar { display: none; }
.pchip {
  flex: 0 0 auto;
  border: 1px solid rgba(180, 160, 255, 0.35);
  background: rgba(80, 60, 140, 0.22);
  color: var(--text);
  border-radius: 999px;
  padding: 6px 10px;
  font: inherit;
  font-size: 0.78rem;
  cursor: pointer;
  white-space: nowrap;
}
.pchip.active,
.pchip:active {
  border-color: rgba(180, 160, 255, 0.7);
}
.want-modes {
  display: flex;
  gap: 6px;
  max-width: 760px;
  margin: 0 auto 8px;
}
.want-modes .wmode {
  border: 1px solid rgba(61, 255, 181, 0.28);
  background: rgba(0, 40, 30, 0.35);
  color: var(--muted);
  border-radius: 8px;
  padding: 5px 10px;
  font: inherit;
  font-size: 0.78rem;
  cursor: pointer;
  transition: border-color 0.15s ease, color 0.15s ease, background 0.15s ease;
}
.want-modes .wmode:hover {
  color: #9dffd4;
  border-color: rgba(61, 255, 181, 0.45);
}
.want-modes .wmode.active {
  color: var(--ok);
  border-color: rgba(61, 255, 181, 0.55);
  background: rgba(0, 55, 40, 0.5);
}
.preset-rail,
.quick-chips {
  scrollbar-width: thin;
}

.composer-wrap {
  padding: 8px 18px calc(16px + var(--safe-bottom));
  background: linear-gradient(180deg, transparent, rgba(5, 7, 11, 0.92) 30%);
  padding-bottom: calc(16px + var(--safe-bottom) + var(--vv-offset, 0px));
  transition: padding-bottom 0.12s ease;
}
.composer {
  max-width: 760px;
  margin: 0 auto;
  border: 1px solid var(--line-strong);
  background: color-mix(in srgb, var(--bg-panel) 88%, #000);
  border-radius: 18px;
  padding: 10px 12px 8px;
  box-shadow: var(--shadow);
  display: grid;
  gap: 6px;
}
.composer textarea {
  width: 100%;
  resize: none;
  border: 0;
  outline: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 16px; /* prevent iOS zoom */
  min-height: 44px;
  max-height: 160px;
  padding: 8px 4px;
  line-height: 1.45;
}
.composer-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.composer-bar .pill { margin-right: auto; }
.attach-btn {
  min-width: 40px;
  height: 40px;
  padding: 0 10px;
  border: 1px solid var(--line-strong);
  border-radius: 12px;
  background: linear-gradient(180deg, rgba(0, 229, 255, 0.14), rgba(0, 229, 255, 0.04));
  color: var(--cyan);
  font-size: 0.82rem;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.attach-btn svg { display: block; flex-shrink: 0; }
.attach-btn .attach-label { font-family: var(--font-mono); letter-spacing: 0.02em; }
.attach-btn:hover {
  border-color: var(--cyan);
  color: #041018;
  background: var(--cyan);
}
@media (max-width: 520px) {
  .attach-btn .attach-label { display: none; }
  .attach-btn { width: 40px; padding: 0; justify-content: center; }
}
.attach-chips {
  max-width: 760px;
  margin: 0 auto 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.attach-chips[hidden] { display: none !important; }
.attach-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.78rem;
  font-family: var(--font-mono);
  color: var(--text);
  background: var(--cyan-soft);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 4px 8px 4px 10px;
  max-width: 100%;
}
.attach-chip span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 220px;
}
.attach-chip button {
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  padding: 0 2px;
}
.attach-chip button:hover { color: var(--text); }
.pill {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  color: var(--cyan);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px 10px;
  background: var(--cyan-soft);
}
.send {
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 14px;
  background: var(--cyan);
  color: #041018;
  font-weight: 700;
  font-size: 1.1rem;
  cursor: pointer;
  flex-shrink: 0;
}
.send:disabled { opacity: 0.45; cursor: not-allowed; }
.composer-hint {
  max-width: 760px;
  margin: 8px auto 0;
  text-align: center;
  color: var(--muted);
  font-size: 0.72rem;
}
.status {
  max-width: 760px;
  margin: 0 auto 8px;
  color: var(--danger);
  font-size: 0.85rem;
}

/* —— Tools drawer / essentials (slim composer) —— */
.tools-toggle {
  margin-left: auto;
  border: 1px solid var(--line-strong);
  background: rgba(0, 229, 255, 0.1);
  color: var(--cyan);
  border-radius: 8px;
  padding: 5px 10px;
  font: inherit;
  font-size: 0.72rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.tools-toggle[aria-expanded="true"] {
  background: rgba(0, 229, 255, 0.22);
  border-color: var(--cyan);
}
.tools-drawer {
  display: none;
  max-width: 760px;
  margin: 0 auto 6px;
  max-height: min(32vh, 240px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 6px 0 2px;
  border-top: 1px solid rgba(0, 229, 255, 0.12);
}
.tools-drawer.open { display: block; }
.tools-drawer[hidden] { display: none !important; }
.tools-section-label {
  margin: 8px 2px 4px;
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted, #8a9aab);
  opacity: 0.9;
}
.tools-section-label:first-child { margin-top: 2px; }
.composer-essentials {
  display: flex;
  flex-wrap: nowrap;
  gap: 6px;
  overflow-x: auto;
  max-width: 760px;
  margin: 0 auto 8px;
  padding: 0 2px;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.composer-essentials::-webkit-scrollbar { display: none; }
.composer-essentials .qchip {
  min-height: 34px;
  padding: 5px 12px;
  font-size: 0.8rem;
}
/* Композер: не раздувать drawer секциями */
.tools-drawer .preset-rail,
.tools-drawer .quick-chips {
  margin-bottom: 4px;
}
.tools-drawer .pchip,
.tools-drawer .qchip {
  font-size: 0.78rem;
}
body.side-open { overflow: hidden; overscroll-behavior: none; }

/* —— Mobile / tablet —— */
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(88vw, 320px);
    z-index: 40;
    transform: translateX(-105%);
    transition: transform 0.22s ease;
    box-shadow: var(--shadow);
    padding-bottom: calc(14px + var(--safe-bottom));
  }
  .sidebar.open { transform: translateX(0); }
  .mobile-only { display: inline-grid; }
  .side-close { margin-left: auto; }
  .desktop-hint { display: none; }
  .mobile-hint { display: block; }
  .topbar {
    padding-left: 12px;
    padding-right: 12px;
    gap: 8px;
    min-height: 48px;
  }
  .top-meta { display: none; }
  .turn { padding-left: 12px; padding-right: 12px; }
  .turn.user .bubble { max-width: 95%; }
  .composer-wrap {
    padding-left: 10px;
    padding-right: 10px;
    padding-bottom: calc(10px + var(--safe-bottom) + var(--vv-offset, 0px));
    position: sticky;
    bottom: 0;
    z-index: 15;
  }
  .composer {
    border-radius: 16px;
    padding: 8px 10px 6px;
  }
  .composer textarea {
    min-height: 40px;
    max-height: min(28vh, 140px);
    padding: 6px 2px;
  }
  .messages {
    padding-top: 12px;
    padding-bottom: 8px;
  }
  .want-modes {
    width: 100%;
    max-width: none;
    padding: 0 2px;
    align-items: center;
  }
  .want-modes .wmode {
    flex: 1 1 0;
    text-align: center;
    min-height: 36px;
    padding: 6px 4px;
  }
  .tools-toggle {
    display: inline-flex;
    align-items: center;
    flex: 0 0 auto;
    min-height: 36px;
  }
  .tools-drawer.open {
    max-height: min(52vh, 400px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding-bottom: 8px;
  }
  .tools-drawer .tools-section-label:first-child {
    margin-top: 2px;
  }
  .composer-essentials {
    gap: 6px;
    max-width: 100%;
  }
  .composer-essentials .qchip {
    flex: 0 0 auto;
  }
  .tools-section-label {
    font-size: 0.68rem;
    letter-spacing: 0.04em;
    margin: 8px 2px 4px;
    opacity: 0.72;
  }
  .composer-essentials .qchip,
  .preset-rail .pchip,
  .quick-chips .qchip {
    min-height: 36px;
    padding: 6px 12px;
    font-size: 0.8rem;
  }
  .fab-scroll {
    bottom: calc(120px + var(--safe-bottom) + var(--vv-offset, 0px));
    right: calc(12px + var(--safe-right));
    width: 44px;
    height: 44px;
  }
  .agent-rail {
    max-width: none;
    margin-bottom: 6px;
  }
  .attach-chip span { max-width: 140px; }
  .status {
    font-size: 0.8rem;
    padding: 0 2px;
  }
}

@media (max-width: 420px) {
  body { font-size: 14px; }
  .brand-name { font-size: 0.95rem; }
  .composer textarea { max-height: min(24vh, 110px); }
  .want-modes .wmode { font-size: 0.72rem; }
  .tools-toggle { font-size: 0.68rem; padding: 5px 8px; }
  .composer-hint { font-size: 0.68rem; }
  .top-title {
    font-size: 0.92rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .composer-essentials .qchip {
    padding: 6px 10px;
    font-size: 0.76rem;
  }
  /* На узких экранах убрать лишнее из частых — картинку спрячем в «Ещё» */
  .composer-essentials .qchip[data-prompt="нарисуй "] {
    display: none;
  }
  .agent-select {
    max-width: 42vw;
    font-size: 0.78rem;
  }
  .composer-bar {
    gap: 6px;
  }
  .attach-label { display: none; }
  .composer-hint { max-width: 96vw; }
}

/* Landscape phones: keep composer usable, shorter drawer */
@media (max-width: 900px) and (max-height: 480px) and (orientation: landscape) {
  .composer-wrap {
    padding-bottom: calc(6px + var(--safe-bottom) + var(--vv-offset, 0px));
  }
  .composer textarea {
    min-height: 32px;
    max-height: min(22vh, 88px);
  }
  .tools-drawer.open {
    max-height: min(28vh, 160px);
  }
  .composer-essentials { margin-bottom: 4px; }
  .fab-scroll {
    bottom: calc(88px + var(--safe-bottom) + var(--vv-offset, 0px));
  }
  .messages { padding-top: 8px; }
}

@media (hover: none) and (pointer: coarse) {
  .side-btn:hover { background: color-mix(in srgb, var(--cyan-soft) 80%, transparent); }
  .hist-item:active { background: var(--cyan-soft); }
  .pchip:active,
  .qchip:active,
  .wmode:active {
    transform: scale(0.98);
  }
  .send,
  .attach-btn,
  .icon-btn {
    min-width: 44px;
    min-height: 44px;
  }
}

/* —— Login modal —— */
.modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(2, 5, 10, 0.72);
  backdrop-filter: blur(6px);
}
.modal[hidden] { display: none !important; }
.modal-card {
  width: min(420px, 100%);
  border: 1px solid var(--line-strong);
  border-radius: 18px;
  background: color-mix(in srgb, var(--bg-panel) 92%, #000);
  box-shadow: var(--shadow);
  padding: 20px 18px 16px;
}
.modal-card h2 {
  margin: 0 0 6px;
  font-size: 1.15rem;
}
.modal-hint {
  margin: 0 0 14px;
  color: var(--muted);
  font-size: 0.86rem;
  line-height: 1.4;
}
.modal-card .field {
  display: grid;
  gap: 6px;
  margin-bottom: 10px;
  font-size: 0.86rem;
  color: var(--muted);
}
.modal-card .field input {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.28);
  color: var(--text);
  padding: 10px 12px;
  font: inherit;
  font-size: 16px;
}
.modal-card .field input:focus {
  outline: none;
  border-color: var(--line-strong);
}
.modal-err {
  color: #ffb3c0;
  font-size: 0.84rem;
  margin: 0 0 10px;
}
.modal-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 8px;
}
.modal-actions .side-btn {
  width: auto;
  min-width: 110px;
  justify-content: center;
}

/* —— Generation structure animation —— */
.gen-structure {
  display: grid;
  gap: 10px;
  padding: 4px 2px 2px;
  min-width: min(420px, 100%);
}
.gen-head {
  display: flex;
  align-items: center;
  gap: 10px;
}
.gen-spin {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid rgba(0, 229, 255, 0.25);
  border-top-color: var(--cyan);
  animation: gen-spin 0.85s linear infinite;
}
@keyframes gen-spin { to { transform: rotate(360deg); } }
.gen-kicker {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  color: var(--cyan);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
.gen-graph {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
}
.gen-node {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  padding: 4px 8px;
  border-radius: 8px;
  border: 1px solid var(--line);
  color: var(--muted);
  background: rgba(0, 0, 0, 0.25);
}
.gen-node.is-active {
  color: #041018;
  background: var(--cyan);
  border-color: var(--cyan);
  box-shadow: 0 0 16px rgba(0, 229, 255, 0.35);
  animation: gen-pulse 1.1s ease-in-out infinite;
}
.gen-node.is-done {
  color: var(--ok);
  border-color: rgba(61, 255, 181, 0.45);
  background: rgba(61, 255, 181, 0.08);
}
.gen-edge {
  width: 14px;
  height: 2px;
  background: linear-gradient(90deg, rgba(0,229,255,0.15), rgba(0,229,255,0.55));
  border-radius: 2px;
}
.gen-line {
  color: var(--text);
  font-size: 0.92rem;
}
.gen-bars {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
  height: 8px;
}
.gen-bars i {
  display: block;
  border-radius: 4px;
  background: rgba(0, 229, 255, 0.18);
  animation: gen-bar 1.2s ease-in-out infinite;
}
.gen-bars i:nth-child(2) { animation-delay: 0.12s; }
.gen-bars i:nth-child(3) { animation-delay: 0.24s; }
.gen-bars i:nth-child(4) { animation-delay: 0.36s; }
.gen-bars i:nth-child(5) { animation-delay: 0.48s; }
@keyframes gen-bar {
  0%, 100% { opacity: 0.35; transform: scaleY(0.55); }
  50% { opacity: 1; transform: scaleY(1); background: var(--cyan); }
}
@keyframes gen-pulse {
  0%, 100% { box-shadow: 0 0 10px rgba(0, 229, 255, 0.25); }
  50% { box-shadow: 0 0 22px rgba(0, 229, 255, 0.55); }
}
.bubble.generating { min-height: 88px; }
