:root {
      --bg: #f6f7fb;
      --surface: #ffffff;
      --surface-muted: #eef0f5;
      --border: #e2e5ec;
      --text: #1a1d26;
      --text-muted: #5c6370;
      --accent: #5b6cf0;
      --accent-hover: #4a5ad9;
      --user-bubble: #e8f0fe;
      --user-border: #c5d9fc;
      --ai-bubble: #ffffff;
      --ai-border: #dde1ea;
      --green: #0d9f4f;
      --warn-bg: #fff8e6;
      --warn-border: #f0d090;
      --system-ok: #0d8a4a;
      --shadow: 0 1px 3px rgba(15, 23, 42, 0.08);
    }

    * { box-sizing: border-box; margin: 0; padding: 0; }

    html,
    body {
      width: 100%;
      height: 100%;
      min-height: 100%;
    }

    body {
      font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
      background: var(--bg);
      color: var(--text);
      height: var(--app-height, 100vh);
      min-height: var(--app-height, 100vh);
      display: flex;
      flex-direction: column;
      overflow: hidden;
      overscroll-behavior: none;
      -webkit-tap-highlight-color: transparent;
    }

    /* ——— Top bar ——— */
    .topbar {
      display: flex;
      align-items: center;
      gap: 12px;
      padding: max(10px, env(safe-area-inset-top)) 18px 10px;
      background: var(--surface);
      border-bottom: 1px solid var(--border);
      min-height: 52px;
      flex-shrink: 0;
      box-shadow: var(--shadow);
    }

    .brand {
      display: flex;
      align-items: center;
      gap: 10px;
      font-weight: 700;
      font-size: 17px;
      color: var(--text);
    }

    .brand-icon {
      width: 28px;
      height: 28px;
      display: flex;
      align-items: center;
      justify-content: center;
      background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
      border-radius: 8px;
      color: #fff;
      flex-shrink: 0;
    }

    .brand-icon svg { width: 16px; height: 16px; }

    /* Логотип + выбор модели слева с ровным интервалом (как в макете) */
    .topbar-leading {
      display: flex;
      align-items: center;
      gap: 20px;
      flex: 0 1 auto;
      min-width: 0;
    }

    .topbar-spacer { flex: 1; min-width: 8px; }

    .model-select {
      padding: 5px 8px;
      border-radius: 6px;
      border: 1px solid var(--border);
      background: var(--surface-muted);
      color: var(--text);
      font-family: "Roboto Condensed", "Arial Narrow", "Segoe UI", system-ui, -apple-system, sans-serif;
      font-size: 12px;
      font-weight: 400;
      letter-spacing: 0.01em;
      line-height: 1.25;
      cursor: pointer;
      max-width: min(calc(448px * 1.1 * 1.05 * 1.05), calc(58vw * 0.8 * 1.1 * 1.05 * 1.05));
    }

    .model-select option {
      font-family: "Roboto Condensed", "Arial Narrow", "Segoe UI", system-ui, -apple-system, sans-serif;
      font-size: 12px;
    }

    .topbar-actions {
      display: flex;
      align-items: center;
      gap: 8px;
    }

    .topbar-btn {
      width: 36px;
      height: 36px;
      min-width: 36px;
      padding: 0;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      border-radius: 10px;
      border: 1px solid var(--border);
      background: var(--surface);
      color: var(--text-muted);
      cursor: pointer;
      font-size: 18px;
      line-height: 1;
      box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
      transition: background 0.15s, color 0.15s, border-color 0.15s;
    }

    .topbar-btn:hover {
      background: var(--surface-muted);
      color: var(--text);
      border-color: var(--accent);
    }

    /* ——— Main chat ——— */
    .main {
      flex: 1;
      display: flex;
      flex-direction: column;
      min-height: 0;
      overflow: hidden;
    }

    .chat-area {
      flex: 1;
      display: flex;
      flex-direction: column;
      min-height: 0;
      background: var(--bg);
    }

    .messages {
      flex: 1;
      overflow-y: auto;
      padding: 20px 24px 8px;
      display: flex;
      flex-direction: column;
      gap: 14px;
    }

    .msg {
      max-width: 85%;
      padding: 12px 14px;
      border-radius: 12px;
      font-size: 14px;
      line-height: 1.5;
    }

    .msg.user {
      align-self: flex-end;
      background: var(--user-bubble);
      border: 1px solid var(--user-border);
      color: var(--text);
      min-width: 260px;
    }

    .msg.ai {
      align-self: flex-start;
      background: var(--ai-bubble);
      border: 1px solid var(--ai-border);
      color: var(--text);
      box-shadow: var(--shadow);
    }

    .msg.system-ok {
      align-self: center;
      max-width: 90%;
      background: transparent;
      border: none;
      color: var(--system-ok);
      font-size: 13px;
      text-align: center;
    }

    .msg.system-warn {
      align-self: center;
      max-width: 90%;
      background: var(--warn-bg);
      border: 1px solid var(--warn-border);
      color: #7a5a10;
      font-size: 13px;
    }

    .msg-meta {
      margin-top: 10px;
      font-size: 11px;
      color: var(--text-muted);
      display: flex;
      flex-wrap: wrap;
      gap: 8px;
    }

    .strategy-tag {
      display: inline-block;
      font-size: 11px;
      font-weight: 700;
      color: var(--accent);
      margin-bottom: 8px;
      letter-spacing: 0.02em;
    }

    .msg.ai.kie-attempt {
      max-width: 92%;
    }

    .kie-error {
      color: #b91c1c;
      font-size: 13px;
      white-space: pre-wrap;
      word-break: break-word;
    }

    .kie-loading {
      align-self: center;
      font-size: 13px;
      color: var(--text-muted);
      padding: 8px 12px;
    }

    .send-btn:disabled {
      opacity: 0.55;
      cursor: not-allowed;
    }

    .msg.user,
    .msg.ai.kie-attempt {
      position: relative;
      padding-bottom: 8px;
    }

    .msg-text-wrap {
      white-space: pre-wrap;
      word-break: break-word;
    }

    .code-block-lite {
      border: 1px solid var(--border);
      border-radius: 10px;
      overflow: hidden;
      margin-top: 8px;
      background: #f8fafc;
    }

    .code-block-head {
      display: flex;
      align-items: center;
      justify-content: space-between;
      gap: 8px;
      padding: 6px 10px;
      background: #eef2ff;
      border-bottom: 1px solid #dbe2f5;
    }

    .code-block-lang {
      font-size: 12px;
      color: #4338ca;
      font-family: ui-monospace, Consolas, monospace;
    }

    .code-copy-btn {
      border: 1px solid var(--border);
      border-radius: 7px;
      background: var(--surface);
      color: var(--text-muted);
      font-size: 12px;
      padding: 3px 10px;
      cursor: pointer;
    }

    .code-copy-btn:hover {
      background: var(--accent);
      border-color: var(--accent);
      color: #fff;
    }

    .code-block-body {
      margin: 0;
      padding: 12px;
      overflow-x: auto;
      white-space: pre;
      font-size: 13px;
      line-height: 1.5;
      font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
      color: #0f172a;
      background: #f8fafc;
    }

    .no-code-highlight .code-block-lite {
      border: none;
      background: transparent;
      margin: 0;
      border-radius: 0;
      overflow: visible;
    }
    .no-code-highlight .code-block-head {
      display: none;
    }
    .no-code-highlight .code-block-body {
      padding: 0;
      white-space: pre-wrap;
      overflow-wrap: anywhere;
      word-break: normal;
      overflow: visible;
      font-family: inherit;
      font-size: inherit;
      line-height: inherit;
      color: inherit;
      background: transparent;
    }

    /* pre-wrap + word-break:break-word давали лишние «пустые» строки в WebKit при длинных токенах */
    .wrap-code-lines .code-block-body {
      white-space: pre-wrap;
      overflow-wrap: anywhere;
      word-break: normal;
      overflow-x: visible;
      line-break: auto;
    }

    .msg-actions-inner {
      position: absolute;
      display: flex;
      gap: 4px;
      align-items: center;
      opacity: 0;
      transition: opacity 0.2s;
      z-index: 2;
      bottom: 2px;
      right: 8px;
      padding: 0;
      background: transparent;
      border-radius: 0;
      border: none;
      box-shadow: none;
    }

    .msg.user:hover .msg-actions-inner,
    .msg.ai.kie-attempt:hover .msg-actions-inner {
      opacity: 1;
    }

    .action-btn {
      padding: 2px 6px;
      font-size: 13px;
      border: 1px solid var(--border);
      border-radius: 4px;
      background: var(--surface);
      cursor: pointer;
      color: var(--text-muted);
      line-height: 1.2;
    }

    .action-btn:hover {
      background: var(--accent);
      border-color: var(--accent);
      color: #fff;
    }

    .msg-actions-inner .msg-time-hint {
      font-size: 10px;
      color: var(--text-muted);
      margin-right: 4px;
      max-width: 120px;
      white-space: nowrap;
    }

    .copied-flash {
      font-size: 10px;
      color: var(--system-ok);
      display: none;
      margin-right: 4px;
    }

    .copied-flash.visible {
      display: inline;
    }

    .cost-footer {
      margin-top: 10px;
      padding-top: 8px;
      border-top: 1px solid var(--border);
      font-size: 11px;
      line-height: 1.5;
      position: relative;
      cursor: default;
    }

    .cost-footer-line {
      display: flex;
      flex-wrap: wrap;
      align-items: baseline;
      gap: 0 10px;
    }

    .cost-orange {
      color: #d97706;
      font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
      font-weight: 600;
      cursor: help;
    }

    .cost-footer.has-tooltip:hover .cost-orange {
      text-decoration: none;
    }

    .cost-muted {
      color: var(--text-muted);
      font-size: 11px;
      font-family: inherit;
    }

    .cost-tooltip-lite {
      display: none;
      position: absolute;
      left: 0;
      bottom: calc(100% + 6px);
      background: var(--surface-muted);
      border: 1px solid #c9cfdb;
      border-radius: 8px;
      padding: 12px 14px;
      font-size: 11px;
      color: var(--text);
      z-index: 2147483647;
      min-width: 300px;
      max-width: 420px;
      box-shadow: 0 8px 24px rgba(15, 23, 42, 0.12);
      line-height: 1.55;
      font-family: ui-monospace, Consolas, monospace;
    }

    .cost-footer.has-tooltip:hover .cost-tooltip-lite {
      display: block;
    }

    .cost-tooltip-lite .tt-header {
      color: var(--accent);
      font-weight: 600;
      margin-bottom: 6px;
    }

    .cost-tooltip-lite .tt-row {
      display: flex;
      justify-content: space-between;
      gap: 16px;
    }

    .cost-tooltip-lite .tt-label {
      color: var(--text-muted);
    }

    .cost-tooltip-lite .tt-value {
      text-align: right;
    }

    .cost-tooltip-lite .tt-sep {
      border-top: 1px solid var(--border);
      margin: 6px 0;
    }

    .cost-tooltip-lite .tt-total {
      color: #d97706;
      font-weight: 600;
    }

    .scroll-bottom-btn {
      position: absolute;
      right: 28px;
      bottom: 120px;
      width: 36px;
      height: 36px;
      border-radius: 50%;
      border: 1px solid var(--border);
      background: var(--surface);
      cursor: pointer;
      box-shadow: var(--shadow);
      opacity: 0.85;
    }

    .scroll-bottom-btn:hover { opacity: 1; }

    .chat-area { position: relative; }

    .chat-artifacts-fab {
      position: relative;
      z-index: 6;
      display: flex;
      flex-direction: column;
      align-items: flex-end;
      gap: 4px;
      flex-shrink: 0;
    }

    .chat-artifacts-btn {
      width: 36px;
      height: 36px;
      border-radius: 10px;
      border: 1px solid var(--border);
      background: var(--surface);
      cursor: pointer;
      font-size: 18px;
      line-height: 1;
      box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
    }

    .chat-artifacts-btn:hover {
      border-color: var(--accent);
    }

    .chat-artifacts-popover {
      position: absolute;
      top: 100%;
      right: 0;
      margin-top: 6px;
      max-width: min(320px, 92vw);
      padding: 8px 10px;
      background: var(--surface);
      border: 1px solid var(--border);
      border-radius: 10px;
      box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
      z-index: 25;
    }

    /* ——— Bottom input ——— */
    .bottom-input-wrapper {
      border-top: 1px solid var(--border);
      background: var(--surface);
      padding: 6px 16px calc(12px + env(safe-area-inset-bottom));
      flex-shrink: 0;
    }

    .bottom-input-wrapper.drop-zone-active-compact .input-area {
      outline: 2px solid var(--accent);
      outline-offset: -2px;
      box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.12);
    }

    .bottom-input-wrapper.drop-zone-active-expanded {
      outline: 2px solid var(--accent);
      outline-offset: -2px;
      background: var(--surface-muted);
    }

    .attachments-container {
      display: flex;
      flex-wrap: wrap;
      gap: 4px;
      min-height: 0;
    }

    .attachments-container:not(:empty) {
      margin-bottom: 3px;
    }

    .attachments-summary {
      display: none;
      align-items: center;
      justify-content: space-between;
      gap: 12px;
      margin-bottom: 6px;
      font-size: 12px;
      color: var(--text-muted);
    }

    .attachments-summary.visible {
      display: flex;
    }

    .attachments-summary .left {
      min-width: 0;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    .attachments-summary .clear-btn {
      border: none;
      background: none;
      color: #c02626;
      cursor: pointer;
      padding: 0;
      font-size: 12px;
      line-height: 1.2;
      text-decoration: underline;
      text-underline-offset: 2px;
      white-space: nowrap;
    }

    .attachments-summary .clear-btn:hover {
      color: #991b1b;
    }

    .attachment-chip {
      display: flex;
      align-items: center;
      gap: 4px;
      padding: 3px 4px;
      background: transparent;
      border: 1px solid var(--border);
      border-radius: 10px;
      font-size: 12px;
      color: var(--text);
      position: relative;
      max-width: 360px;
    }

    .attachment-chip .thumb {
      width: 36px;
      height: 36px;
      object-fit: cover;
      border-radius: 6px;
      flex-shrink: 0;
    }

    .attachment-chip .info { min-width: 0; }
    .attachment-chip .name {
      font-weight: 600;
      white-space: normal;
      overflow: visible;
      text-overflow: clip;
      word-break: break-all;
      line-height: 1.15;
    }
    .attachment-chip .size { color: var(--text-muted); font-size: 10px; line-height: 1.15; }

    .attachment-chip .remove {
      margin-left: auto;
      border: none;
      background: none;
      cursor: pointer;
      color: var(--text-muted);
      padding: 2px;
      line-height: 1;
      font-size: 12px;
    }
    .attachment-chip .remove:hover { color: #c00; }

    .attachment-chip:hover .hover-preview {
      display: block;
    }

    .hover-preview {
      display: none;
      position: absolute;
      left: 0;
      bottom: 100%;
      margin-bottom: 8px;
      z-index: 100;
      background: var(--surface);
      border: 1px solid var(--border);
      border-radius: 10px;
      box-shadow: 0 8px 24px rgba(15, 23, 42, 0.15);
      padding: 8px;
      max-width: 280px;
      pointer-events: none;
    }

    .hover-preview img {
      max-width: 260px;
      max-height: 180px;
      display: block;
      border-radius: 6px;
    }

    .hover-preview pre {
      margin: 0;
      font-size: 11px;
      line-height: 1.4;
      white-space: pre-wrap;
      word-break: break-word;
      max-height: 120px;
      overflow: hidden;
      color: var(--text-muted);
    }

    .input-area {
      display: flex;
      align-items: center;
      gap: 10px;
      background: var(--surface-muted);
      border: 1px solid var(--border);
      border-radius: 12px;
      padding: 6px 10px;
    }

    .mode-switch-bottom {
      display: flex;
      gap: 4px;
      align-items: center;
    }

    .mode-btn-bottom {
      width: 36px;
      height: 36px;
      border-radius: 8px;
      border: 1px solid var(--border);
      background: var(--surface);
      cursor: pointer;
      font-size: 16px;
      line-height: 1;
      color: var(--text-muted);
    }

    .mode-btn-bottom.active {
      background: var(--accent);
      border-color: var(--accent);
      color: #fff;
    }

    .input-area textarea {
      flex: 1;
      border: none;
      background: transparent;
      resize: none;
      font-family: inherit;
      font-size: 14px;
      line-height: 1.45;
      min-height: 36px;
      max-height: 160px;
      padding: 6px 4px;
      color: var(--text);
    }

    .input-area textarea:focus { outline: none; }

    .attach-file-input-hidden {
      position: absolute;
      width: 0;
      height: 0;
      opacity: 0;
      pointer-events: none;
    }

    .prompt-input-wrap {
      flex: 1;
      min-width: 0;
      position: relative;
      align-self: stretch;
    }

    .prompt-input-wrap textarea {
      width: 100%;
      box-sizing: border-box;
    }

    .attach-plus-wrap {
      position: absolute;
      left: 4px;
      top: 8px;
      z-index: 2;
    }

    .attach-overlay-btn {
      display: none;
      position: relative;
      width: 32px;
      height: 32px;
      padding: 0;
      border-radius: 8px;
      border: 1px solid var(--border);
      background: var(--surface);
      cursor: pointer;
      font-size: 20px;
      font-weight: 300;
      line-height: 1;
      color: var(--text-muted);
      align-items: center;
      justify-content: center;
    }

    .attach-overlay-btn:active {
      background: var(--surface-muted);
    }

    .attach-sheet {
      display: none;
      position: absolute;
      left: 0;
      top: calc(100% + 4px);
      flex-direction: column;
      min-width: min(220px, calc(100vw - 32px));
      background: var(--surface);
      border: 1px solid var(--border);
      border-radius: 10px;
      box-shadow: 0 8px 24px rgba(15, 23, 42, 0.14);
      z-index: 40;
      overflow: hidden;
    }

    .attach-sheet.open {
      display: flex;
    }

    .attach-sheet__opt {
      display: block;
      width: 100%;
      margin: 0;
      padding: 12px 14px;
      border: none;
      border-bottom: 1px solid var(--border);
      background: var(--surface);
      font-size: 14px;
      font-family: inherit;
      text-align: left;
      cursor: pointer;
      color: var(--text);
    }

    .attach-sheet__opt:last-child {
      border-bottom: none;
    }

    .attach-sheet__opt:active {
      background: var(--surface-muted);
    }

.estimate-btn {
      width: 40px;
      height: 40px;
      border-radius: 8px;
      border: 1px solid var(--border);
      background: var(--surface);
      cursor: pointer;
      font-size: 18px;
      flex-shrink: 0;
    }

    .estimate-btn:hover { background: var(--surface-muted); }

    .mic-btn {
      width: 40px;
      height: 40px;
      border-radius: 8px;
      border: 1px solid var(--border);
      background: var(--surface);
      cursor: pointer;
      font-size: 18px;
      color: var(--text-muted);
      flex-shrink: 0;
      transition: background 0.15s, border-color 0.15s, color 0.15s, box-shadow 0.15s;
    }

    .mic-btn:hover:not(:disabled):not(.is-active) { background: var(--surface-muted); }

    .mic-btn.is-active {
      background: linear-gradient(135deg, #7482ff 0%, #8b5cf6 100%);
      border-color: #7482ff;
      color: #fff;
      box-shadow: 0 0 0 1px rgba(116, 130, 255, 0.45);
    }

    .mic-btn:disabled {
      opacity: 0.65;
      cursor: not-allowed;
    }

    .feature-btn {
      width: 60px;
      height: 40px;
      border-radius: 8px;
      border: 1px solid var(--border);
      background: var(--surface);
      cursor: pointer;
      font-size: 11px;
      font-weight: 600;
      line-height: 1;
      color: var(--text-muted);
      flex-shrink: 0;
      transition: background 0.15s, border-color 0.15s, color 0.15s, box-shadow 0.15s;
    }

    .feature-btn:hover:not(.is-active) { background: var(--surface-muted); }

    .feature-btn.is-active {
      background: linear-gradient(135deg, #4f84ff 0%, #7a5cff 100%);
      border-color: #4f84ff;
      color: #fff;
      box-shadow: 0 0 0 1px rgba(79, 132, 255, 0.45);
    }

    .send-btn {
      padding: 0 18px;
      height: 40px;
      border-radius: 8px;
      border: none;
      background: var(--accent);
      color: #fff;
      font-weight: 600;
      font-size: 13px;
      cursor: pointer;
      flex-shrink: 0;
    }

    .send-btn:hover { background: var(--accent-hover); }

    /* ——— Balance bar ——— */
    .balance-bar {
      display: flex;
      align-items: center;
      gap: 12px;
      padding: 8px 18px max(8px, env(safe-area-inset-bottom));
      background: var(--surface);
      border-top: 1px solid var(--border);
      font-size: 12px;
      color: var(--text-muted);
      flex-shrink: 0;
    }

    .balance-item.kieai .bal-label { font-weight: 600; color: var(--text); }
    .balance-item.kieai .bal-value { color: #6b5bb8; font-weight: 600; }
    .balance-item.google .bal-label { font-weight: 600; color: var(--text); }
    .balance-item.google .bal-value { color: #4ea1ff; font-weight: 600; }

    .balance-session {
      margin-left: auto;
      color: var(--text-muted);
      position: relative;
      cursor: default;
    }

    .balance-session .total {
      font-weight: 700;
      color: var(--text);
    }

    .balance-session:hover .session-tooltip,
    .session-tooltip:hover {
      display: block;
    }

    .session-tooltip {
      display: none;
      position: absolute;
      right: 0;
      bottom: calc(100% - 2px);
      margin-bottom: 0;
      width: min(560px, 86vw);
      max-height: 280px;
      overflow-y: auto;
      background: var(--surface);
      border: 1px solid var(--border);
      border-radius: 10px;
      box-shadow: 0 12px 26px rgba(15, 23, 42, 0.14);
      padding: 10px;
      z-index: 2147483647;
    }

    .session-tooltip::after {
      content: "";
      position: absolute;
      left: 0;
      right: 0;
      bottom: -10px;
      height: 10px;
      background: transparent;
    }

    .session-tooltip .st-empty {
      color: var(--text-muted);
      font-size: 12px;
      padding: 6px 4px;
    }

    .session-tooltip .st-header {
      font-size: 12px;
      font-weight: 700;
      color: var(--text);
      margin-bottom: 8px;
    }

    .session-tooltip .st-row {
      display: grid;
      grid-template-columns: 112px 1fr 70px 82px;
      gap: 8px;
      align-items: center;
      border-top: 1px solid var(--border);
      padding: 6px 0;
      font-size: 12px;
      cursor: pointer;
      transition: background 0.15s, border-color 0.15s;
    }

    .session-tooltip .st-row:hover,
    .session-tooltip .st-row.active {
      background: #f6f8ff;
      border-color: #cfd7ff;
    }

    .session-tooltip .st-time,
    .session-tooltip .st-tokens {
      color: var(--text-muted);
      font-family: ui-monospace, Consolas, monospace;
    }

    .session-tooltip .st-model {
      color: var(--text);
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    .session-tooltip .st-cost {
      color: #c2410c;
      font-weight: 700;
      text-align: right;
      font-family: ui-monospace, Consolas, monospace;
    }

    .session-cost-popup {
      position: fixed;
      display: none;
      z-index: 2147483647;
      background: var(--surface-muted);
      border: 1px solid #c9cfdb;
      border-radius: 8px;
      padding: 12px 14px;
      font-size: 11px;
      color: var(--text);
      min-width: 300px;
      max-width: 420px;
      box-shadow: 0 8px 24px rgba(15, 23, 42, 0.18);
      line-height: 1.55;
      font-family: ui-monospace, Consolas, monospace;
    }

    .session-cost-popup .tt-header {
      color: var(--accent);
      font-weight: 600;
      margin-bottom: 6px;
    }

    .session-cost-popup .tt-row {
      display: flex;
      justify-content: space-between;
      gap: 16px;
    }

    .session-cost-popup .tt-label {
      color: var(--text-muted);
    }

    .session-cost-popup .tt-value {
      text-align: right;
    }

    .session-cost-popup .tt-sep {
      border-top: 1px solid var(--border);
      margin: 6px 0;
    }

    .session-cost-popup .tt-total {
      color: #d97706;
      font-weight: 600;
    }

    .request-status {
      display: none;
      align-items: center;
      gap: 10px;
      position: static;
      z-index: 1;
      padding: 0;
      margin-top: 0;
      margin-bottom: 4px;
      transform: translateY(8px);
      font-size: 14px;
      color: var(--text-muted);
      background: transparent;
    }

    .request-status.active {
      display: flex;
    }

    .request-cancel-btn {
      border: 1px solid #fecaca;
      border-radius: 999px;
      background: #fff;
      color: #b91c1c;
      cursor: pointer;
      font-size: 14px;
      padding: 2px 10px;
      line-height: 1.2;
    }

    .request-cancel-btn:hover {
      border-color: #ef4444;
      background: #fff1f2;
    }

    .request-dots {
      color: var(--accent);
      letter-spacing: 3px;
      font-weight: 700;
      font-size: 14px;
      animation: dotsBlink 1.1s infinite;
      transform: translateY(-1px);
    }

    .request-timer {
      color: #d97706;
      font-family: ui-monospace, Consolas, monospace;
      font-weight: 600;
      font-size: 14px;
    }

    .msg-attachments {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
      column-gap: 7px;
      row-gap: 3px;
      margin-bottom: 8px;
    }

    .msg-attachment-chip {
      display: inline-flex;
      align-items: center;
      gap: 6px;
      width: 100%;
      max-width: none;
      padding: 1px 2px;
      border: none;
      border-radius: 0;
      background: transparent;
      color: var(--text-muted);
      font-size: 11px;
      position: relative;
    }

    .msg-attachment-chip.is-image {
      cursor: pointer;
    }

    .msg-attachment-chip .thumb {
      width: 20px;
      height: 20px;
      border-radius: 4px;
      object-fit: cover;
      flex-shrink: 0;
    }

    .msg-attachment-chip .file-icon {
      font-size: 13px;
      line-height: 1;
    }

    .msg-attachment-chip .name {
      min-width: 0;
      overflow: visible;
      text-overflow: clip;
      white-space: nowrap;
      word-break: normal;
      font-weight: 600;
      color: var(--text);
    }

    .msg-attachment-chip:hover .msg-attachment-hover-preview {
      display: block;
    }

    .msg-attachment-hover-preview {
      display: none;
      position: absolute;
      left: 0;
      bottom: 100%;
      margin-bottom: 8px;
      z-index: 120;
      background: var(--surface);
      border: 1px solid var(--border);
      border-radius: 10px;
      box-shadow: 0 8px 24px rgba(15, 23, 42, 0.15);
      padding: 8px;
      max-width: 300px;
      pointer-events: none;
    }

    .msg-attachment-hover-preview img {
      max-width: 280px;
      max-height: 190px;
      display: block;
      border-radius: 6px;
    }

    .msg-attachment-hover-preview pre {
      margin: 0;
      font-size: 11px;
      line-height: 1.35;
      white-space: pre-wrap;
      word-break: break-word;
      max-height: 130px;
      overflow: hidden;
      color: var(--text-muted);
    }

    .ai-attachment-ref {
      display: inline-flex;
      align-items: center;
      gap: 6px;
      margin: 0 4px;
      padding: 1px 2px;
      border-radius: 6px;
      background: rgba(99, 102, 241, 0.06);
      vertical-align: middle;
      position: relative;
    }

    .ai-attachment-ref.is-image {
      cursor: pointer;
    }

    .ai-attachment-ref .thumb {
      width: 20px;
      height: 20px;
      border-radius: 4px;
      object-fit: cover;
      flex-shrink: 0;
    }

    .ai-attachment-ref .file-icon {
      font-size: 13px;
      line-height: 1;
    }

    .ai-attachment-ref .name {
      font-size: 12px;
      font-weight: 600;
      color: var(--text);
      white-space: nowrap;
    }

    .ai-attachment-ref:hover .ai-attachment-hover-preview {
      display: block;
    }

    .ai-attachment-hover-preview {
      display: none;
      position: absolute;
      left: 0;
      bottom: 100%;
      margin-bottom: 8px;
      z-index: 140;
      background: var(--surface);
      border: 1px solid var(--border);
      border-radius: 10px;
      box-shadow: 0 8px 24px rgba(15, 23, 42, 0.15);
      padding: 8px;
      max-width: 300px;
      pointer-events: none;
    }

    .ai-attachment-hover-preview img {
      max-width: 280px;
      max-height: 190px;
      display: block;
      border-radius: 6px;
    }

    .ai-attachment-hover-preview pre {
      margin: 0;
      font-size: 11px;
      line-height: 1.35;
      white-space: pre-wrap;
      word-break: break-word;
      max-height: 130px;
      overflow: hidden;
      color: var(--text-muted);
    }

    @keyframes dotsBlink {
      0%, 100% { opacity: 0.25; }
      50% { opacity: 1; }
    }

    /* ——— Modals ——— */
    .modal-overlay {
      display: none;
      position: fixed;
      inset: 0;
      background: rgba(15, 23, 42, 0.35);
      z-index: 1000;
      align-items: center;
      justify-content: center;
      padding: 16px;
    }

    .modal-overlay.open { display: flex; }

    /* Короткое уведомление без клика (сброс сессии и т.п.) */
    .app-toast {
      position: fixed;
      left: 50%;
      bottom: calc(96px + env(safe-area-inset-bottom));
      transform: translateX(-50%) translateY(10px);
      max-width: min(92vw, 380px);
      padding: 11px 16px;
      background: var(--text);
      color: var(--surface);
      border-radius: 10px;
      font-size: 14px;
      line-height: 1.35;
      text-align: center;
      box-shadow: 0 8px 28px rgba(15, 23, 42, 0.22);
      z-index: 1200;
      opacity: 0;
      transition: opacity 0.22s ease, transform 0.22s ease;
      pointer-events: none;
    }

    .app-toast--visible {
      opacity: 0.97;
      transform: translateX(-50%) translateY(0);
    }

    #settingsHelpModal {
      z-index: 1100;
    }

    .settings-help-body {
      padding: 16px 20px;
      overflow-y: auto;
      max-height: min(60vh, 420px);
      font-size: 14px;
      line-height: 1.5;
      color: var(--text);
      white-space: pre-wrap;
      word-break: break-word;
    }

    .modal-footer.settings-help-footer {
      padding: 12px 20px 18px;
      justify-content: flex-end;
    }

    button.settings-help-label {
      display: block;
      margin: 12px 0 6px;
      font-size: 13px;
      font-weight: 600;
      color: var(--text);
      background: none;
      border: none;
      padding: 0;
      text-align: left;
      cursor: pointer;
      font-family: inherit;
      width: 100%;
      line-height: 1.35;
    }

    .main-tab-content > button.settings-help-label:first-of-type,
    .tab-content > button.settings-help-label:first-of-type {
      margin-top: 0;
    }

    .setting-row .settings-help-label {
      flex: 1;
      margin: 0;
      min-width: 0;
    }

    button.settings-help-label.settings-help-sub {
      font-size: 12px;
      font-weight: 500;
      color: var(--text-muted);
      margin: 6px 0 10px;
      width: auto;
    }

    button.settings-help-label:hover,
    button.settings-help-label:focus-visible {
      color: var(--accent);
      text-decoration: underline;
      text-underline-offset: 2px;
      outline: none;
    }

    button.settings-help-label.settings-help-heading {
      margin: 0 0 10px;
      font-size: 14px;
      font-weight: 700;
      width: 100%;
    }

    .strategy-pref-box .settings-help-heading {
      margin-bottom: 10px;
    }

    .strategy-pref-box button.settings-help-label:not(.settings-help-heading) {
      margin-top: 10px;
      margin-bottom: 6px;
    }

    .strategy-pref-box button.settings-help-label:not(.settings-help-heading):first-of-type {
      margin-top: 0;
    }

    button.settings-help-label.settings-help-inline {
      display: inline;
      width: auto;
      margin: 0;
      font-weight: 400;
      font-size: 12px;
      color: var(--text-muted);
      vertical-align: baseline;
    }

    .modal {
      background: var(--surface);
      border-radius: 14px;
      border: 1px solid var(--border);
      box-shadow: 0 20px 50px rgba(15, 23, 42, 0.18);
      width: min(560px, 100%);
      max-height: 90vh;
      overflow: hidden;
      display: flex;
      flex-direction: column;
    }

    .modal.wide { width: min(720px, 100%); }

    .modal-header {
      padding: 18px 20px 12px;
      border-bottom: 1px solid var(--border);
    }

    .modal-header h2 {
      font-size: 18px;
      font-weight: 700;
      color: var(--text);
      display: flex;
      align-items: center;
      gap: 8px;
    }

    .tabs, .tabs.main-tabs {
      display: flex;
      gap: 6px;
      padding: 10px 16px 0;
      flex-wrap: wrap;
    }

    .tab-btn {
      padding: 8px 14px;
      border-radius: 8px 8px 0 0;
      border: 1px solid transparent;
      border-bottom: none;
      background: var(--surface-muted);
      color: var(--text-muted);
      cursor: pointer;
      font-size: 13px;
      font-weight: 600;
    }

    .tab-btn.active {
      background: var(--surface);
      color: var(--accent);
      border-color: var(--border);
      border-bottom: 1px solid var(--surface);
      margin-bottom: -1px;
    }

    .main-tab-content, .tab-content {
      display: none;
      padding: 16px 20px;
      overflow-y: auto;
      flex: 1;
      border-top: 1px solid var(--border);
    }

    .main-tab-content.active, .tab-content.active { display: block; }

    .main-tab-content label,
    .tab-content label {
      display: block;
      margin: 12px 0 6px;
      font-size: 13px;
      font-weight: 600;
      color: var(--text);
    }

    .main-tab-content label:first-of-type { margin-top: 0; }

    .main-tab-content input[type="text"],
    .main-tab-content input[type="password"],
    .tab-content input[type="password"],
    .tab-content select,
    .main-tab-content select {
      width: 100%;
      padding: 10px 12px;
      border-radius: 8px;
      border: 1px solid var(--border);
      background: var(--surface-muted);
      font-size: 13px;
      color: var(--text);
    }

    .setting-row {
      display: flex;
      align-items: center;
      justify-content: space-between;
      gap: 12px;
      margin-top: 14px;
    }

    .setting-row input[type="range"] { flex: 1; max-width: 200px; }

    .test-btn {
      padding: 8px 14px;
      border-radius: 8px;
      border: 1px solid var(--border);
      background: var(--surface-muted);
      cursor: pointer;
      font-size: 13px;
    }

    .test-btn:hover { background: #e8eaef; }

    .test-btn-cloud {
      background: #eef3ff;
      border-color: #7b9ef0;
      color: #2952c4;
      margin-left: 8px;
    }
    .test-btn-cloud:hover { background: #d9e5ff; }

    .test-btn-danger {
      border-color: #dc2626;
      color: #b91c1c;
      background: #fff1f2;
    }

    .test-btn-danger:hover {
      background: #ffe4e6;
    }

    .test-btn-right {
      margin-left: auto;
    }

    .provider-select-row {
      display: flex;
      flex-wrap: wrap;
      gap: 10px;
      margin-top: 8px;
    }

    .provider-card {
      flex: 1;
      min-width: 140px;
      padding: 12px;
      border-radius: 10px;
      border: 2px solid var(--border);
      background: var(--surface-muted);
      cursor: pointer;
    }

    .provider-card.selected {
      border-color: var(--accent);
      background: #f0f2ff;
    }

    .agent-model-row {
      display: grid;
      grid-template-columns: 1fr 1fr;
      gap: 12px;
      margin-top: 16px;
      padding: 12px;
      border: 1px solid rgba(91, 108, 240, 0.35);
      border-radius: 10px;
      background: #fafbff;
    }

    @media (max-width: 900px) {
      .topbar {
        padding-left: 12px;
        padding-right: 12px;
        gap: 8px;
      }
      .topbar-leading {
        gap: 10px;
        min-width: 0;
      }
      .model-select {
        max-width: min(calc(60vw * 0.8 * 1.1 * 1.05 * 1.05), calc(240px * 1.1 * 1.05 * 1.05));
      }
      .messages {
        padding: 14px 12px 6px;
      }
      .bottom-input-wrapper {
        padding-left: 10px;
        padding-right: 10px;
      }
    }

    @media (max-width: 600px) {
      .agent-model-row { grid-template-columns: 1fr; }
      .topbar {
        padding-left: 4px;
        padding-right: 4px;
        padding-top: max(6px, env(safe-area-inset-top));
        padding-bottom: 6px;
        flex-wrap: nowrap;
        align-items: center;
        row-gap: 0;
        min-height: 44px;
      }
      .topbar-leading {
        flex: 1 1 auto;
        width: auto;
        min-width: 0;
        gap: 2px;
      }
      .brand {
        gap: 3px;
      }
      .brand-icon {
        width: 16px;
        height: 16px;
        border-radius: 5px;
      }
      .brand-icon svg {
        width: 10px;
        height: 10px;
      }
      .brand {
        flex-shrink: 0;
      }
      .topbar-spacer {
        display: none;
      }
      .topbar-actions {
        flex-shrink: 0;
        margin-left: 0;
        align-self: center;
        gap: 6px;
      }
      .model-select {
        flex: 1 1 auto;
        min-width: 0;
        width: auto;
        max-width: min(100%, max(0px, calc((100% - 72px) * 0.8 * 1.1 * 1.05 * 1.05)));
        padding-top: 2px;
        padding-bottom: 2px;
        padding-left: 2px;
        padding-right: 4px;
        font-family: "Roboto Condensed", "Arial Narrow", "Segoe UI", system-ui, -apple-system, sans-serif;
        font-size: 11px;
        font-weight: 400;
        line-height: 1.15;
        letter-spacing: 0.01em;
      }

      .model-select option {
        font-size: 11px;
      }

      .messages {
        padding: 5px 10px 4px;
      }
      .bottom-input-wrapper {
        padding-top: 5px;
      }
      .input-area {
        flex-wrap: wrap;
        align-items: stretch;
        gap: 6px;
        padding: 6px;
      }
      .mode-switch-bottom,
      .mic-btn,
      .feature-btn,
      .estimate-btn,
      .send-btn {
        order: 2;
      }
      .prompt-input-wrap {
        order: 1;
        flex: 1 1 100%;
        width: 100%;
        min-width: 0;
      }

      .input-area textarea,
      .prompt-input-wrap textarea {
        width: 100%;
        min-width: 0;
        max-height: 120px;
        min-height: 34px;
        padding: 6px 6px 4px 36px;
        box-sizing: border-box;
      }

      .attach-plus-wrap {
        left: 0;
        top: 3px;
      }

      .attach-overlay-btn {
        display: inline-flex;
        width: 30px;
        height: 30px;
        font-size: 18px;
      }
      .feature-btn {
        width: 48px;
        height: 36px;
        font-size: 10px;
      }
      .mic-btn,
      .estimate-btn,
      .mode-btn-bottom {
        width: 36px;
        height: 36px;
      }
      .send-btn {
        flex: 1 1 0;
        min-width: 0;
        width: auto;
        max-width: none;
        height: 36px;
        padding: 0 12px;
        white-space: nowrap;
      }
      .session-tooltip {
        width: min(520px, 94vw);
      }
    }

    .agent-model-label { font-size: 12px; margin-bottom: 6px; display: block; }

    /* System prompt (settings tab) */
    .system-prompt-textarea {
      width: 100%;
      min-height: 220px;
      resize: vertical;
      font-family: inherit;
      font-size: 13px;
      line-height: 1.45;
      padding: 10px 12px;
      border-radius: 10px;
      border: 1px solid var(--border);
      background: var(--bg);
      color: var(--text);
      box-sizing: border-box;
    }

    .system-global-prompt-textarea {
      width: 100%;
      min-height: 150px;
      resize: vertical;
      font-family: inherit;
      font-size: 13px;
      line-height: 1.45;
      padding: 10px 12px;
      border-radius: 10px;
      border: 1px solid var(--border);
      background: var(--bg);
      color: var(--text);
      box-sizing: border-box;
    }

    .system-prompt-select {
      width: 100%;
      min-width: 220px;
      max-width: 520px;
    }

    .modal-footer {
      display: flex;
      justify-content: flex-end;
      gap: 10px;
      padding: 14px 20px;
      border-top: 1px solid var(--border);
      background: var(--surface-muted);
    }

    .modal-footer.settings-footer {
      justify-content: space-between;
    }

    .btn-cancel {
      padding: 9px 18px;
      border-radius: 8px;
      border: 1px solid var(--border);
      background: var(--surface);
      cursor: pointer;
      font-size: 13px;
    }

    .btn-save {
      padding: 9px 20px;
      border-radius: 8px;
      border: none;
      background: var(--accent);
      color: #fff;
      font-weight: 600;
      cursor: pointer;
      font-size: 13px;
    }

    .btn-save:hover { background: var(--accent-hover); }

    /* Estimate modal */
    .estimate-modal .modal-header h2 { margin: 0; align-items: baseline; }
    .estimate-time-header {
      font-size: 11px;
      color: #c02626;
      font-weight: 500;
      margin-left: auto;
    }

    .estimate-list {
      border: 1px solid var(--border);
      border-radius: 10px;
      overflow: hidden;
    }

    .estimate-table {
      width: 100%;
      border-collapse: collapse;
      font-size: 13px;
    }

    .estimate-table th {
      text-align: left;
      padding: 10px 12px;
      background: var(--surface-muted);
      color: var(--text-muted);
      font-weight: 600;
      border-bottom: 1px solid var(--border);
    }

    .estimate-table th:nth-child(2),
    .estimate-table th:nth-child(3) { text-align: center; }

    .estimate-table td {
      padding: 12px;
      border-bottom: 1px solid var(--border);
      vertical-align: middle;
    }

    .estimate-table tr:last-child td { border-bottom: none; }

    .estimate-table td:nth-child(2) { text-align: center; }
    .estimate-table td:nth-child(3) { text-align: center; }

    .model-cell strong { display: block; color: var(--text); }
    .model-cell small { color: var(--text-muted); font-size: 11px; }

    .credits-hint { font-size: 10px; color: #6b5bb8; display: block; margin-bottom: 2px; }
    .price-val { color: var(--green); font-weight: 600; }

    .estimate-disclaimer {
      font-size: 11px;
      color: var(--text-muted);
      margin-top: 10px;
    }

    .estimate-modal .modal-footer {
      justify-content: space-between;
      align-items: center;
    }

    .btn-ai-advice {
      border: none;
      background: none;
      color: var(--accent);
      cursor: pointer;
      font-size: 13px;
      display: flex;
      align-items: center;
      gap: 6px;
    }

    .btn-ai-advice:hover { text-decoration: underline; }

    .test-row {
      display: flex;
      align-items: center;
      gap: 12px;
      margin-top: 12px;
    }

    .test-row-stack {
      align-items: flex-start;
    }

    .test-row-clear-result {
      margin-top: 4px;
    }

    .test-result {
      font-size: 12px;
      color: var(--text-muted);
      line-height: 1.4;
      white-space: pre-line;
    }

    .test-result.ok {
      color: #0d8a4a;
      font-weight: 600;
    }

    .test-result.err {
      color: #b91c1c;
      font-weight: 600;
    }

    #testModelsResult {
      display: block;
      white-space: pre-wrap;
    }

    .strategy-pref-box {
      margin-top: 12px;
      padding: 12px;
      border: 1px solid var(--border);
      border-radius: 10px;
      background: var(--surface);
    }

    .strategy-pref-box .settings-hint {
      margin-top: 8px;
      font-size: 12px;
    }

    /* Полноэкранный просмотр вложенного изображения (как в VibeStudio) */
    .image-overlay {
      display: none;
      position: fixed;
      inset: 0;
      background: rgba(0, 0, 0, 0.5);
      z-index: 2147483647;
      justify-content: center;
      align-items: center;
      cursor: pointer;
    }

    .image-overlay.is-open {
      display: flex;
    }

    .image-modal-content {
      background: var(--surface);
      border: 1px solid var(--border);
      border-radius: 12px;
      display: flex;
      flex-direction: column;
      overflow: hidden;
      box-shadow: 0 10px 40px rgba(0, 0, 0, 0.25);
      cursor: default;
      max-width: 95vw;
      max-height: 95vh;
    }

    .image-modal-header {
      padding: 8px 16px;
      background: var(--surface-muted);
      border-bottom: 1px solid var(--border);
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 13px;
      font-weight: 600;
      color: var(--text);
    }

    .image-modal-header .image-name {
      color: var(--accent);
      font-family: ui-monospace, monospace;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      max-width: 70vw;
    }

    .image-modal-header .close-btn {
      cursor: pointer;
      color: var(--text-muted);
      font-size: 18px;
      line-height: 1;
      padding: 4px 8px;
      border: none;
      background: none;
    }

    .image-modal-header .close-btn:hover {
      color: #c02626;
    }

    .image-modal-body {
      padding: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      overflow: auto;
      background: #0f0f12;
    }

    .image-modal-body img {
      display: block;
      object-fit: contain;
    }

    .attachment-chip.is-image {
      cursor: pointer;
    }

    .settings-hint {
      font-size: 12px;
      color: var(--text-muted);
      line-height: 1.45;
      margin-top: 6px;
      max-width: 48em;
    }

    .settings-hint + label {
      margin-top: 14px;
    }
