/* ============================================================
   Ospaces — AI Assistant (drawer + key modal)
   Scoped under .ai-root. Reuses the app's CSS variables so it
   inherits light/dark theming automatically.
   ============================================================ */
.ai-root {
  position: fixed; inset: 0; z-index: 250;
  font-family: var(--font);
  color: var(--text);
}
.ai-root .ai-ic { display: block; flex-shrink: 0; }
.ai-root [hidden] { display: none !important; }
.ai-mono { font-family: var(--font-mono); font-size: 0.92em; }

.ai-backdrop {
  position: absolute; inset: 0;
  background: rgba(15, 15, 15, 0.32);
  animation: ai-fade 0.14s ease;
}
.ai-root.ai-closing { pointer-events: none; }
.ai-root.ai-closing .ai-backdrop,
.ai-root.ai-closing .ai-panel,
.ai-root.ai-closing .ai-modal,
.ai-root.ai-closing .ai-inline { opacity: 0; transition: opacity 0.15s ease; }
@keyframes ai-fade { from { opacity: 0; } }

/* ============================================================
   Shared controls
   ============================================================ */
.ai-btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 11px; border-radius: 6px;
  font-size: 13px; font-weight: 500; font-family: var(--font);
  color: var(--text-light); background: var(--bg);
  border: 1px solid var(--border-strong); cursor: pointer;
  white-space: nowrap;
}
.ai-btn:hover { background: var(--bg-hover); color: var(--text); }
.ai-btn:disabled { opacity: 0.5; cursor: default; }
.ai-btn.ai-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.ai-btn.ai-primary:hover { background: var(--accent-hover); }

.ai-input {
  width: 100%; padding: 9px 11px; font-size: 14px; font-family: var(--font);
  border: 1px solid var(--border-strong); border-radius: 7px;
  background: var(--bg-input); color: var(--text); outline: none;
}
.ai-input:focus { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(91, 91, 214, 0.22); }

.ai-x {
  display: inline-flex; align-items: center; justify-content: center;
  width: 28px; height: 28px; border-radius: 5px;
  color: var(--text-light); background: none; border: none; cursor: pointer;
}
.ai-x:hover { background: var(--bg-hover); color: var(--text); }

/* ============================================================
   Key modal
   ============================================================ */
.ai-modal-root { display: flex; align-items: flex-start; justify-content: center; }
.ai-modal {
  position: relative; margin-top: 12vh;
  width: 440px; max-width: calc(100vw - 32px);
  background: var(--bg-modal); border-radius: 12px;
  box-shadow: var(--shadow-popover);
  overflow: hidden; animation: ai-modal-in 0.14s ease;
}
@keyframes ai-modal-in { from { opacity: 0; transform: translateY(-6px) scale(0.99); } }
.ai-modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 14px 12px 18px; border-bottom: 1px solid var(--border);
}
.ai-modal-title { display: inline-flex; align-items: center; gap: 9px; font-size: 15px; font-weight: 600; }
.ai-modal-body { padding: 16px 18px 18px; }
.ai-modal-note { font-size: 13.5px; line-height: 1.5; color: var(--text-light); margin-bottom: 14px; }
.ai-modal-note b { color: var(--text); font-weight: 600; }
.ai-modal-hint { font-size: 12px; color: var(--text-faint); margin-top: 8px; }
.ai-modal-foot { display: flex; justify-content: space-between; gap: 10px; margin-top: 18px; }

/* ============================================================
   Assistant drawer
   ============================================================ */
.ai-panel-root { display: block; }
.ai-panel {
  position: absolute; top: 0; right: 0; bottom: 0;
  width: 440px; max-width: 100vw;
  display: flex; flex-direction: column;
  background: var(--bg); border-left: 1px solid var(--border);
  box-shadow: rgba(15, 15, 15, 0.16) -8px 0 24px;
  transform: translateX(24px); opacity: 0;
  transition: transform 0.2s cubic-bezier(0.22, 1, 0.36, 1), opacity 0.2s ease;
}
.ai-panel.ai-in { transform: translateX(0); opacity: 1; }

.ai-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 12px 14px 18px; border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  background: linear-gradient(180deg, color-mix(in srgb, var(--accent) 7%, var(--bg)), var(--bg));
}
.ai-title { display: inline-flex; align-items: center; gap: 9px; font-size: 15.5px; font-weight: 650; letter-spacing: -0.1px; }
.ai-title .ai-ic {
  color: #fff; background: var(--accent); border-radius: 8px; padding: 4px;
  width: 26px; height: 26px; box-sizing: border-box;
  box-shadow: 0 2px 6px color-mix(in srgb, var(--accent) 40%, transparent), inset 0 1px 0 rgba(255,255,255,0.25);
}
.ai-head-right { display: flex; align-items: center; gap: 6px; }
.ai-model {
  font-family: var(--font); font-size: 12.5px; color: var(--text-light);
  background: var(--bg-input); border: 1px solid var(--border-strong);
  border-radius: 7px; padding: 6px 8px; outline: none; cursor: pointer; max-width: 190px;
  transition: border-color 0.12s;
}
.ai-model:hover { border-color: var(--accent-soft); }
.ai-model:focus { border-color: var(--accent); }

/* tabs (Write / Ask workspace / Agent) — segmented control */
.ai-tabs {
  display: flex; gap: 3px;
  margin: 12px 12px 0; padding: 3px; flex-shrink: 0;
  background: var(--bg-callout); border: 1px solid var(--border); border-radius: 10px;
}
.ai-tab {
  flex: 1; padding: 6px 10px; border-radius: 7px;
  font-size: 12.75px; font-weight: 550; font-family: var(--font);
  color: var(--text-light); background: none;
  border: 1px solid transparent; cursor: pointer;
  transition: background 0.12s, color 0.12s, box-shadow 0.12s;
}
.ai-tab:hover { color: var(--text); }
.ai-tab.ai-tab-active {
  background: var(--bg-modal); color: var(--text);
  box-shadow: 0 1px 3px rgba(20, 22, 43, 0.1), 0 0 0 1px var(--border);
}

/* quick actions */
.ai-actions {
  display: grid; grid-template-columns: 1fr 1fr; gap: 7px;
  padding: 12px; flex-shrink: 0; position: relative;
}
.ai-actions.ai-busy { opacity: 0.55; pointer-events: none; }
.ai-action {
  display: flex; align-items: center; gap: 9px;
  padding: 9px 11px; border-radius: 9px; text-align: left;
  font-size: 13px; font-weight: 550; color: var(--text);
  background: var(--bg-modal); border: 1px solid var(--border);
  cursor: pointer; min-width: 0;
  transition: background 0.12s, border-color 0.12s, transform 0.08s, box-shadow 0.12s;
}
.ai-action:hover {
  background: var(--bg-hover); border-color: var(--accent-soft);
  box-shadow: 0 2px 8px rgba(20, 22, 43, 0.06);
}
.ai-action:active { transform: translateY(1px); }
.ai-action-emoji {
  font-size: 15px; line-height: 1; flex-shrink: 0;
  width: 24px; height: 24px; display: inline-flex; align-items: center; justify-content: center;
  border-radius: 7px; background: color-mix(in srgb, var(--accent) 9%, transparent);
}
.ai-action-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ai-action-caret { color: var(--text-faint); display: inline-flex; }

/* sub-menu (tones / languages) */
.ai-choice {
  position: absolute; z-index: 10; min-width: 180px; max-height: 260px; overflow-y: auto;
  background: var(--bg-modal); border-radius: 8px; box-shadow: var(--shadow-popover);
  padding: 4px; animation: ai-fade 0.1s ease;
}
.ai-choice-item {
  display: block; width: 100%; text-align: left;
  padding: 7px 10px; border-radius: 5px; font-size: 13.5px; color: var(--text);
  background: none; border: none; cursor: pointer; font-family: var(--font);
}
.ai-choice-item:hover { background: var(--bg-hover); }

/* ============================================================
   Conversation thread (multi-turn chat)
   ============================================================ */
.ai-thread {
  flex: 1; min-height: 0; overflow-y: auto;
  padding: 14px 12px 6px;
  display: flex; flex-direction: column; gap: 12px;
  scroll-behavior: smooth;
}
.ai-thread-empty {
  margin: auto; max-width: 260px; text-align: center;
  color: var(--text-faint); font-size: 13.5px; font-style: italic; line-height: 1.5;
}
.ai-msg-row { display: flex; flex-direction: column; gap: 6px; max-width: 100%; }
.ai-msg-row.ai-msg-user { align-items: flex-end; }
.ai-msg-row.ai-msg-ai { align-items: stretch; }
.ai-msg {
  font-size: 14px; line-height: 1.55; color: var(--text);
  word-break: break-word; white-space: pre-wrap;
}
/* user bubble — compact, accent-tinted, right-aligned */
.ai-msg-u {
  max-width: 85%;
  padding: 9px 13px; border-radius: 14px 14px 4px 14px;
  background: var(--accent); color: #fff;
  box-shadow: 0 1px 3px color-mix(in srgb, var(--accent) 35%, transparent);
}
/* assistant bubble — full-width card */
.ai-msg-a {
  padding: 12px 14px; border-radius: 4px 14px 14px 14px;
  background: var(--bg-code); border: 1px solid var(--border);
}
.ai-msg.ai-md { white-space: normal; }
.ai-msg.ai-error { color: var(--danger); background: color-mix(in srgb, var(--danger) 7%, var(--bg-code)); border-color: color-mix(in srgb, var(--danger) 30%, var(--border)); }
.ai-msg.ai-streaming::after {
  content: "▋"; color: var(--accent); animation: ai-blink 1s steps(2) infinite; margin-left: 1px;
}
/* per-message action bar */
.ai-msg-bar { display: flex; flex-wrap: wrap; gap: 6px; padding-left: 2px; }
.ai-msg-row .ai-sources { padding: 2px 2px 4px; }

/* header controls added for the conversation */
.ai-newchat {
  display: inline-flex; align-items: center; justify-content: center;
  width: 28px; height: 28px; border-radius: 6px;
  color: var(--text-light); background: none; border: 1px solid var(--border-strong); cursor: pointer;
  transition: background 0.12s, color 0.12s;
}
.ai-newchat:hover { background: var(--bg-hover); color: var(--text); }
.ai-newchat[hidden] { display: none; }
.ai-stop-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 32px; height: 32px; flex-shrink: 0; border-radius: 8px;
  background: var(--danger); color: #fff; border: none; cursor: pointer;
}
.ai-stop-btn[hidden] { display: none; }

/* output (legacy — kept for the inline /ai card which still uses .ai-output) */
.ai-output-wrap { flex: 1; display: flex; flex-direction: column; min-height: 0; padding: 0 12px 8px; }
.ai-output {
  flex: 1; overflow-y: auto; min-height: 80px;
  padding: 14px; border-radius: 8px;
  background: var(--bg-code); border: 1px solid var(--border);
  font-size: 14px; line-height: 1.55; color: var(--text);
  white-space: pre-wrap; word-break: break-word;
}
.ai-output.ai-empty { color: var(--text-faint); font-style: italic; display: flex; align-items: center; justify-content: center; text-align: center; }
.ai-output.ai-error { color: var(--danger); }
.ai-output.ai-streaming::after {
  content: "▋"; color: var(--accent); animation: ai-blink 1s steps(2) infinite; margin-left: 1px;
}
@keyframes ai-blink { 50% { opacity: 0; } }

.ai-result-bar { display: flex; flex-wrap: wrap; gap: 6px; padding-top: 8px; }
.ai-result-bar .ai-stop { color: var(--danger); border-color: var(--border-strong); }

/* sources (Ask workspace citations) */
.ai-sources { display: flex; flex-direction: column; gap: 2px; padding-top: 8px; flex-shrink: 0; }
.ai-sources-title {
  font-size: 11px; font-weight: 600; letter-spacing: 0.05em; text-transform: uppercase;
  color: var(--text-faint); margin: 2px 0 3px 2px;
}
.ai-source {
  display: flex; align-items: center; gap: 8px; width: 100%;
  padding: 6px 8px; border-radius: 6px; text-align: left;
  font-size: 13px; font-family: var(--font); color: var(--text);
  background: none; border: none; cursor: pointer; min-width: 0;
}
.ai-source:hover { background: var(--bg-hover); }
.ai-source:disabled { cursor: default; opacity: 0.75; }
.ai-source:disabled:hover { background: none; }
.ai-source-num {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 18px; height: 18px; padding: 0 4px; flex-shrink: 0;
  border-radius: 5px; background: var(--bg-code); border: 1px solid var(--border);
  color: var(--text-light); font-size: 11px; font-weight: 600;
}
.ai-source-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* chat */
.ai-chat { flex-shrink: 0; padding: 10px 12px 12px; border-top: 1px solid var(--border); }
.ai-ctx-toggle {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--text-light); margin-bottom: 8px; cursor: pointer; user-select: none;
}
.ai-ctx-toggle input { accent-color: var(--accent); cursor: pointer; }
.ai-chat-row {
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--bg-input); border: 1px solid var(--border-strong);
  border-radius: 10px; padding: 6px 6px 6px 10px;
}
.ai-chat-row:focus-within { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(91, 91, 214, 0.18); }
.ai-chat-input {
  flex: 1; resize: none; border: none; outline: none; background: none;
  font-family: var(--font); font-size: 14px; line-height: 1.45; color: var(--text);
  max-height: 140px; padding: 4px 0;
}
.ai-chat-input::placeholder { color: var(--text-placeholder); }
.ai-send {
  display: inline-flex; align-items: center; justify-content: center;
  width: 32px; height: 32px; flex-shrink: 0; border-radius: 8px;
  background: var(--accent); color: #fff; border: none; cursor: pointer;
}
.ai-send:hover { background: var(--accent-hover); }
.ai-send:disabled { opacity: 0.5; cursor: default; }

/* ============================================================
   Inline AI prompt (/ai slash command)
   ============================================================ */
.ai-inline-root { background: transparent; }
.ai-inline {
  position: absolute;
  width: min(520px, calc(100vw - 24px));
  display: flex; flex-direction: column; gap: 8px;
  padding: 8px;
  background: var(--bg-modal); border: 1px solid var(--border);
  border-radius: 10px; box-shadow: var(--shadow-popover);
  animation: ai-modal-in 0.12s ease;
}
.ai-inline-center { left: 50%; top: 18vh; transform: translateX(-50%); }
.ai-inline-row { display: flex; align-items: center; gap: 8px; padding-left: 4px; }
.ai-inline-spark { display: inline-flex; flex-shrink: 0; color: var(--accent); }
.ai-inline-input {
  flex: 1; min-width: 0; padding: 4px 0;
  border: none; outline: none; background: none;
  font-family: var(--font); font-size: 14px; color: var(--text);
}
.ai-inline-input::placeholder { color: var(--text-placeholder); }
.ai-inline-input:disabled { opacity: 0.6; }
.ai-inline-preview {
  max-height: 260px; overflow-y: auto;
  padding: 10px 12px; border-radius: 8px;
  background: var(--bg-code); border: 1px solid var(--border);
  font-size: 13.5px; line-height: 1.55; color: var(--text);
  white-space: pre-wrap; word-break: break-word;
}
.ai-inline-preview.ai-error { color: var(--danger); }
.ai-inline-preview.ai-streaming::after {
  content: "▋"; color: var(--accent); animation: ai-blink 1s steps(2) infinite; margin-left: 1px;
}
.ai-inline-bar { display: flex; flex-wrap: wrap; gap: 6px; }
.ai-inline-bar .ai-stop { color: var(--danger); border-color: var(--border-strong); }

/* ============================================================
   Dark theme overrides
   ============================================================ */
html[data-theme="dark"] .ai-backdrop { background: rgba(0, 0, 0, 0.55); }
html[data-theme="dark"] .ai-panel { box-shadow: rgba(0, 0, 0, 0.45) -8px 0 24px; }
html[data-theme="dark"] .ai-inline { border-color: var(--border-strong); }
html[data-theme="dark"] .ai-source-num { border-color: var(--border-strong); }

/* ============================================================
   Responsive
   ============================================================ */
@media (max-width: 480px) {
  .ai-panel { width: 100vw; border-left: none; }
  .ai-actions { grid-template-columns: 1fr; }
  .ai-inline { width: calc(100vw - 24px); }
  .ai-inline-center { left: 12px; transform: none; }
}

/* ============================== provider manager (multi-provider) ============================== */
.ai-modal.ai-modal-lg { width: 620px; }
.ai-modal.ai-modal-lg .ai-modal-body.ai-pm-mount { padding: 0; max-height: 74vh; overflow-y: auto; }

.ai-pm { padding: 16px 18px 18px; }
.ai-pm-note { font-size: 13px; line-height: 1.5; color: var(--text-light); margin: 0 0 14px; }
.ai-pm-note b { color: var(--text); font-weight: 600; }

.ai-pm-default {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; margin-bottom: 14px;
  background: var(--bg-callout); border: 1px solid var(--border); border-radius: var(--radius-ctl);
}
.ai-pm-default label { font-size: 12.5px; font-weight: 600; color: var(--text-light); white-space: nowrap; }
.ai-pm-default-sel { flex: 1; }

.ai-pm-list { display: flex; flex-direction: column; gap: 10px; }
.ai-pm-card {
  border: 1px solid var(--border); border-radius: var(--radius-ctl);
  padding: 12px; background: var(--bg-modal);
}
.ai-pm-card.ai-pm-off { opacity: 0.62; }
.ai-pm-card-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.ai-pm-name { font-weight: 600; font-size: 14px; }
.ai-pm-proto {
  font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.03em;
  color: var(--text-faint); border: 1px solid var(--border); border-radius: 5px; padding: 1px 6px;
}
.ai-pm-status {
  font-size: 11.5px; font-weight: 600; color: var(--text-faint);
  border: 1px solid var(--border); border-radius: 20px; padding: 1px 9px;
}
.ai-pm-status.on { color: #0f9d58; border-color: rgba(15, 157, 88, 0.4); background: rgba(15, 157, 88, 0.09); }
.ai-pm-enable { margin-left: auto; display: inline-flex; align-items: center; gap: 5px; font-size: 12.5px; color: var(--text-light); cursor: pointer; }

.ai-pm-keyrow { display: flex; gap: 8px; margin-bottom: 10px; }
.ai-pm-key { flex: 1; font-family: var(--font-mono); font-size: 12.5px; }

/* Test-connection result line (under the key row). */
.ai-pm-test:empty { display: none; }
.ai-pm-test {
  margin: -4px 0 10px; font-size: 12px; font-weight: 600; line-height: 1.4;
  color: var(--text-light); word-break: break-word;
}
.ai-pm-test.ok { color: #0f9d58; }
.ai-pm-test.err { color: #d44c47; }

.ai-pm-modelrow { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.ai-pm-model-chip {
  display: inline-flex; align-items: center; gap: 5px;
  font-family: var(--font-mono); font-size: 11.5px;
  background: var(--bg-hover); border: 1px solid var(--border); border-radius: 6px; padding: 2px 4px 2px 8px;
}
.ai-pm-chip-x {
  border: none; background: none; color: var(--text-faint); cursor: pointer;
  font-size: 11px; line-height: 1; padding: 2px 3px; border-radius: 4px;
}
.ai-pm-chip-x:hover { color: var(--danger); background: var(--bg-modal); }
.ai-pm-addmodel { flex: 1; min-width: 130px; font-family: var(--font-mono); font-size: 12px; padding: 4px 8px; }

.ai-pm-cardfoot { display: flex; align-items: center; gap: 10px; margin-top: 10px; }
.ai-pm-hint { font-size: 11.5px; color: var(--text-faint); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ai-pm-delprov {
  margin-left: auto; border: 1px solid var(--border); background: none; color: var(--danger);
  font-size: 11.5px; border-radius: 6px; padding: 3px 9px; cursor: pointer; white-space: nowrap;
}
.ai-pm-delprov:hover { background: var(--danger); color: #fff; border-color: var(--danger); }
.ai-pm-provnote { margin-top: 8px; font-size: 11.5px; color: var(--text-light); line-height: 1.45; }

.ai-pm-addbtn { margin-top: 14px; width: 100%; justify-content: center; }
.ai-pm-addform {
  margin-top: 10px; padding: 12px; border: 1px dashed var(--border-strong);
  border-radius: var(--radius-ctl); display: flex; flex-direction: column; gap: 8px;
}
.ai-pm-frow { display: flex; }
.ai-pm-frow .ai-input, .ai-pm-frow select { width: 100%; }
.ai-pm-frow-end { justify-content: flex-end; }

/* ============================== markdown output + agent mode ============================== */
.ai-output.ai-md { white-space: normal; }
.ai-md h3, .ai-md h4, .ai-md h5 { margin: 12px 0 6px; font-weight: 650; line-height: 1.3; }
.ai-md h3 { font-size: 16px; } .ai-md h4 { font-size: 14.5px; } .ai-md h5 { font-size: 13px; }
.ai-md p { margin: 7px 0; line-height: 1.55; }
.ai-md ul, .ai-md ol { margin: 7px 0; padding-left: 20px; }
.ai-md li { margin: 3px 0; line-height: 1.5; }
.ai-md code { font-family: var(--font-mono); font-size: 0.9em; background: var(--bg-hover); padding: 1px 5px; border-radius: 4px; }
.ai-md pre { background: var(--bg-hover); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; overflow-x: auto; margin: 9px 0; }
.ai-md pre code { background: none; padding: 0; }
.ai-md blockquote { border-left: 3px solid var(--border-strong); margin: 8px 0; padding: 2px 0 2px 12px; color: var(--text-light); }
.ai-md a { color: var(--accent); text-decoration: underline; }
.ai-md hr { border: none; border-top: 1px solid var(--border); margin: 12px 0; }
.ai-md > :first-child { margin-top: 0; }
.ai-md > :last-child { margin-bottom: 0; }

/* agent mode: live action chips */
.ai-output.ai-agentlog { display: flex; flex-direction: column; gap: 8px; }
.ai-agent-actions { display: flex; flex-direction: column; gap: 6px; }
.ai-agent-chip {
  display: inline-flex; align-items: center; gap: 8px; align-self: flex-start;
  font-size: 12.5px; color: var(--text-light);
  background: var(--bg-callout); border: 1px solid var(--border);
  border-radius: 999px; padding: 4px 12px; max-width: 100%;
}
.ai-agent-chip::before {
  content: ""; width: 11px; height: 11px; border-radius: 50%; flex: none;
  border: 2px solid var(--border-strong); border-top-color: var(--accent);
  animation: ai-spin 0.7s linear infinite;
}
@keyframes ai-spin { to { transform: rotate(360deg); } }
.ai-agent-chip.done { color: var(--text); border-color: color-mix(in srgb, #0f9d58 42%, var(--border)); }
.ai-agent-chip.done::before { border: none; animation: none; content: "✓"; color: #0f9d58; font-weight: 700; font-size: 12px; width: auto; height: auto; }
.ai-agent-thinking { font-size: 12.5px; color: var(--text-faint); animation: ai-pulse 1.3s ease-in-out infinite; }
@keyframes ai-pulse { 0%, 100% { opacity: 0.5; } 50% { opacity: 1; } }
.ai-agent-final { margin-top: 2px; }

/* ============================================================
   ✦ Ask AI — selection-toolbar action (editor.js + aiedit.js)
   These elements live OUTSIDE .ai-root (they hang off the editor's
   popover layer / body), so the rules below are intentionally unscoped.
   ============================================================ */

/* Toolbar button (sits inside the dark .fmt-toolbar) */
.fmt-btn.fmt-ai {
  gap: 5px; padding: 0 9px; min-width: 0;
  font-size: 12.5px; font-weight: 600; letter-spacing: 0.1px;
  color: #cdd3ff; white-space: nowrap;
}
.fmt-btn.fmt-ai:hover { background: rgba(120, 130, 255, 0.24); color: #fff; }

/* Action menu — reuses the app's .popover shell so it inherits theming */
.ai-edit-menu { min-width: 208px; z-index: 160; }
.ai-edit-menu .menu-item { align-items: center; }
.ai-edit-menu .ai-edit-spark {
  width: 18px; flex-shrink: 0; text-align: center;
  color: var(--accent); font-size: 13px;
}
.ai-edit-menu .ai-edit-caret { margin-left: auto; padding-left: 8px; color: var(--text-faint); font-size: 15px; line-height: 1; }
.ai-edit-nokey { padding: 11px 12px; max-width: 220px; font-size: 12.5px; line-height: 1.45; color: var(--text-light); }

/* Free-form "tell AI what to do" box at the top of the edit menu */
.ai-edit-ask { display: flex; gap: 6px; align-items: center; padding: 6px 6px 4px; }
.ai-edit-ask-input {
  flex: 1; min-width: 0; padding: 7px 10px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg-input, var(--bg)); color: var(--text);
  font-family: var(--font); font-size: 13px; outline: none;
}
.ai-edit-ask-input:focus { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(120, 130, 255, 0.18); }
.ai-edit-ask-send {
  flex: none; width: 30px; height: 30px; border: none; border-radius: 8px; cursor: pointer;
  background: var(--accent); color: #fff; font-size: 14px; line-height: 1;
  display: inline-flex; align-items: center; justify-content: center;
}
.ai-edit-ask-send:hover { filter: brightness(1.08); }
.ai-edit-div { height: 1px; margin: 4px 6px; background: var(--border); }

/* Result card for a free-form AI answer (Replace / Insert below / Copy / …) */
.ai-result-card {
  position: fixed; z-index: 175; width: min(420px, 92vw);
  padding: 12px; display: flex; flex-direction: column; gap: 9px;
  animation: ai-fade 0.14s ease;
}
.ai-result-q {
  font-size: 12px; font-weight: 600; color: var(--accent);
  display: flex; align-items: center; gap: 6px;
}
.ai-result-q::before { content: "✦"; }
.ai-result-body {
  max-height: 46vh; overflow-y: auto; white-space: pre-wrap; word-break: break-word;
  font-size: 13.5px; line-height: 1.5; color: var(--text);
  padding: 8px 10px; border-radius: 8px; background: var(--bg-hover, rgba(127,127,127,.08));
}
.ai-result-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.ai-result-btn {
  padding: 5px 11px; border: 1px solid var(--border); border-radius: 7px; cursor: pointer;
  background: var(--bg-modal); color: var(--text); font-family: var(--font); font-size: 12.5px; font-weight: 500;
}
.ai-result-btn:hover { background: var(--bg-hover, rgba(127,127,127,.1)); }
.ai-result-btn.primary { background: var(--accent); color: #fff; border-color: transparent; }
.ai-result-btn.primary:hover { filter: brightness(1.08); background: var(--accent); }
.ai-result-btn.ai-result-x { margin-left: auto; padding: 5px 9px; color: var(--text-faint); }

/* Translate → language flyout */
.ai-edit-subwrap { position: relative; }
.ai-has-sub { position: relative; }
.ai-has-sub::after { content: ""; position: absolute; top: 0; right: -12px; width: 12px; height: 100%; }
.ai-edit-submenu {
  position: absolute; left: 100%; top: -4px; display: none;
  min-width: 148px; padding: 4px;
  background: var(--bg-modal); border-radius: 12px; box-shadow: var(--shadow-popover);
  z-index: 1;
}
.ai-edit-subwrap:hover .ai-edit-submenu,
.ai-edit-submenu:hover { display: block; }

/* Inline "thinking" chip shown near the selection while the request runs */
.ai-edit-thinking {
  position: fixed; z-index: 170;
  display: inline-flex; align-items: center; gap: 7px;
  padding: 6px 12px; border-radius: 10px;
  background: #22264a; color: #d7daed;
  font-family: var(--font); font-size: 12.5px; font-weight: 500; white-space: nowrap;
  box-shadow: rgba(4, 5, 16, 0.4) 0 6px 18px, rgba(255, 255, 255, 0.08) 0 0 0 1px;
  animation: ai-fade 0.14s ease;
}
html[data-theme="dark"] .ai-edit-thinking { background: #262a4e; }
.ai-edit-spin {
  width: 12px; height: 12px; flex: none; border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.25); border-top-color: #cdd3ff;
  animation: ai-spin 0.7s linear infinite;
}

/* Self-contained fallback note (used when no global toast exists) */
.ai-edit-note {
  position: fixed; left: 50%; bottom: 26px; transform: translate(-50%, 8px);
  z-index: 400; max-width: min(90vw, 420px);
  padding: 10px 16px; border-radius: 10px;
  background: #2b2f4c; color: #f0f2ff;
  font-family: var(--font); font-size: 13.5px; font-weight: 500;
  box-shadow: rgba(4, 5, 16, 0.4) 0 8px 24px, rgba(255, 255, 255, 0.06) 0 0 0 1px;
  opacity: 0; pointer-events: none;
  transition: opacity 0.18s ease, transform 0.18s ease;
}
.ai-edit-note.show { opacity: 1; transform: translate(-50%, 0); }
