/* Asistente de IA: panel lateral, botón para abrirlo y popover sobre el texto seleccionado. */

:root { --ai-w: 420px; }

/* ---------- botones para abrir ---------- */
/* contenedor común de arriba a la derecha (lo crea nav.js; también lo usa la calculadora) */
.top-tools {
  position: fixed;
  top: 14px;
  right: 18px;
  z-index: 35;
  display: flex;
  align-items: center;
  gap: 8px;
}
body.ai-open .top-tools { right: calc(var(--ai-w) + 18px); }
@media (max-width: 960px) { .top-tools { display: none; } }
.ai-fab {
  display: inline-flex;
  height: 36px;
  align-items: center;
  gap: 6px;
  padding: 7px 13px 7px 10px;
  border-radius: 99px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: 600 0.88rem var(--font-sans);
  box-shadow: var(--shadow);
  cursor: pointer;
}
.ai-fab:hover { border-color: var(--accent); color: var(--accent); }
.ai-fab svg, .ai-pop-btn svg, .ai-web svg, .ai-meta svg, .ai-x svg, .ai-send svg {
  width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; display: block;
}
.ai-fab span, .ai-pop-btn span, .ai-web span { display: inline-flex; }
body.ai-open .ai-fab { display: none; }
.ai-mbar-btn { display: none; }
@media (max-width: 960px) {
  .ai-fab { display: none; }
  .ai-mbar-btn { display: inline-grid; }
}

/* ---------- panel ---------- */
.ai-panel {
  position: fixed;
  z-index: 45;
  top: 0;
  right: 0;
  bottom: 0;
  width: var(--ai-w);
  display: flex;
  flex-direction: column;
  background: var(--bg);
  border-left: 1px solid var(--border);
  box-shadow: -8px 0 24px rgb(0 0 0 / 0.06);
}
.ai-panel[hidden] { display: none; }
@media (min-width: 1300px) { body.ai-open .page { padding-right: var(--ai-w); } }
@media (max-width: 600px) { .ai-panel { width: 100%; border-left: 0; } }

.ai-head { display: flex; align-items: center; gap: 6px; padding: 10px 12px; border-bottom: 1px solid var(--border); }
.ai-title { flex: 1; min-width: 0; display: grid; }
.ai-sub {
  justify-self: start;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  border: 0;
  padding: 0;
  background: none;
  color: var(--text-muted);
  font: 0.78rem var(--font-sans);
  cursor: pointer;
}
.ai-sub:hover { color: var(--accent); }

.ai-list, .ai-settings { flex: 1; overflow-y: auto; padding: 14px; }
.ai-list[hidden], .ai-settings[hidden] { display: none; }

/* mensajes */
.ai-msg { margin-bottom: 14px; line-height: 1.6; font-size: 0.94rem; }
.ai-msg.user {
  margin-left: 32px;
  padding: 9px 12px;
  border-radius: 12px 12px 4px 12px;
  background: var(--accent-soft);
}
.ai-msg.user p { margin: 0; white-space: pre-wrap; }
.ai-msg.user .ai-quote + p { margin-top: 6px; }
.ai-quote {
  margin: 0;
  padding: 2px 0 2px 10px;
  border-left: 3px solid var(--accent);
  color: var(--text-muted);
  font-size: 0.86rem;
  white-space: pre-line;
  max-height: 9.5em;
  overflow: auto;
}
.ai-md > :first-child { margin-top: 0; }
.ai-md > :last-child { margin-bottom: 0; }
.ai-md p, .ai-md ul, .ai-md ol { margin: 0 0 0.7em; }
.ai-md ul, .ai-md ol { padding-left: 1.3em; }
.ai-md h1, .ai-md h2, .ai-md h3, .ai-md h4 { font-size: 0.98rem; margin: 1em 0 0.4em; }
.ai-md pre { overflow-x: auto; padding: 10px; border-radius: 8px; background: var(--surface-2); font-size: 0.84rem; }
.ai-md code { background: var(--surface-2); padding: 1px 4px; border-radius: 4px; }
.ai-md pre code { background: none; padding: 0; }
.ai-md table { border-collapse: collapse; font-size: 0.86rem; display: block; overflow-x: auto; }
.ai-md th, .ai-md td { border: 1px solid var(--border); padding: 4px 8px; text-align: left; }
.ai-md blockquote { margin: 0 0 0.7em; padding-left: 10px; border-left: 3px solid var(--border-strong); color: var(--text-muted); }
.ai-md .katex-display { overflow-x: auto; overflow-y: hidden; padding: 2px 0; }
.ai-msg.streaming .ai-md:empty::before { content: "Pensando…"; color: var(--text-muted); animation: ai-pulse 1.2s ease-in-out infinite; }
.ai-msg.streaming .ai-meta { visibility: hidden; }
@keyframes ai-pulse { 50% { opacity: 0.4; } }
@media (prefers-reduced-motion: reduce) { .ai-msg.streaming .ai-md:empty::before { animation: none; } }
.ai-msg.error .ai-md { color: var(--bad); }
.ai-notice { margin: 6px 0 0; font-size: 0.82rem; color: var(--warn); }
.ai-meta { display: flex; align-items: center; gap: 8px; margin-top: 4px; font-size: 0.74rem; color: var(--text-muted); }
.ai-meta button {
  display: inline-flex; align-items: center; gap: 4px;
  border: 0; background: none; padding: 2px; color: var(--text-muted); cursor: pointer; font: inherit;
}
.ai-meta button:hover { color: var(--accent); }
.ai-meta button.done { color: var(--good); }

/* estado vacío */
.ai-empty { color: var(--text-muted); font-size: 0.92rem; }
.ai-empty strong { color: var(--text); }
.ai-suggest { display: grid; gap: 6px; margin-top: 12px; }
.ai-suggest button {
  text-align: left;
  padding: 8px 11px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: 0.88rem var(--font-sans);
  cursor: pointer;
}
.ai-suggest button:hover { border-color: var(--accent); }
.ai-note { padding: 10px 12px; border-radius: 9px; background: var(--surface-2); border: 1px solid var(--border); color: var(--text); font-size: 0.86rem; }
.ai-note p { margin: 0 0 0.5em; }
.ai-note p:last-child { margin: 0; }
.ai-link { border: 0; padding: 0; background: none; color: var(--accent); font: inherit; text-decoration: underline; cursor: pointer; }

/* composer */
.ai-composer { padding: 10px 12px 12px; border-top: 1px solid var(--border); }
.ai-quote-pending { position: relative; margin-bottom: 8px; padding: 8px 30px 8px 10px; border-radius: 9px; background: var(--surface-2); }
.ai-quote-pending[hidden] { display: none; }
.ai-x {
  position: absolute; top: 6px; right: 6px;
  width: 22px; height: 22px; display: grid; place-items: center;
  border: 0; border-radius: 6px; background: none; color: var(--text-muted); cursor: pointer;
}
.ai-x:hover { background: var(--surface); color: var(--text); }
.ai-x svg { width: 14px; height: 14px; }
.ai-box {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 6px 6px 6px 12px;
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  background: var(--surface);
}
.ai-box:focus-within { border-color: var(--accent); }
.ai-input {
  flex: 1;
  min-width: 0;
  border: 0;
  outline: 0;
  resize: none;
  padding: 5px 0;
  background: none;
  color: var(--text);
  font: 0.94rem/1.45 var(--font-sans);
  max-height: 200px;
}
.ai-send {
  flex: none;
  width: 32px; height: 32px; display: grid; place-items: center;
  border: 0; border-radius: 9px; background: var(--accent); color: var(--on-accent); cursor: pointer;
}
.ai-send.stop { background: var(--text); color: var(--bg); }
.ai-foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 7px; font-size: 0.78rem; color: var(--text-muted); }
.ai-ctx { display: inline-flex; align-items: center; gap: 5px; cursor: pointer; }
.ai-web-wrap { position: relative; }
.ai-web {
  display: inline-flex; align-items: center; gap: 4px;
  border: 0; padding: 3px 4px; border-radius: 6px; background: none; color: var(--text-muted); font: inherit; cursor: pointer;
}
.ai-web:hover { color: var(--accent); }
.ai-web svg { width: 14px; height: 14px; }
.ai-menu {
  position: absolute; right: 0; bottom: calc(100% + 4px);
  display: grid; min-width: 130px; padding: 4px;
  border: 1px solid var(--border); border-radius: 9px; background: var(--surface); box-shadow: var(--shadow);
}
.ai-menu[hidden] { display: none; }
.ai-menu button { text-align: left; padding: 6px 9px; border: 0; border-radius: 6px; background: none; color: var(--text); font: 0.86rem var(--font-sans); cursor: pointer; }
.ai-menu button:hover { background: var(--surface-2); }

/* ajustes */
.ai-settings h3 { margin: 16px 0 6px; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--text-muted); }
.ai-settings h3:first-of-type { margin-top: 0; }
.ai-settings .ai-note { margin: 0 0 12px; }
.ai-settings .ai-note.small { margin: 16px 0; }
.ai-settings > .btn.primary { width: 100%; justify-content: center; text-align: center; }
.ai-seg { display: grid; grid-template-columns: repeat(3, 1fr); padding: 3px; gap: 3px; border-radius: 10px; background: var(--surface-2); }
.ai-seg button { padding: 7px; border: 0; border-radius: 8px; background: none; color: var(--text-muted); font: 600 0.88rem var(--font-sans); cursor: pointer; }
.ai-seg button[aria-checked="true"] { background: var(--surface); color: var(--accent); box-shadow: var(--shadow); }
.ai-row { display: flex; gap: 6px; }
.ai-field {
  flex: 1;
  min-width: 0;
  padding: 7px 10px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: 0.88rem var(--font-mono);
}
.ai-field:focus { outline: 0; border-color: var(--accent); }
.ai-settings p.small { margin: 6px 0 0; }

.ai-toast {
  position: absolute; left: 50%; bottom: 96px; transform: translateX(-50%);
  padding: 7px 12px; border-radius: 8px; background: var(--text); color: var(--bg); font-size: 0.84rem; white-space: nowrap;
}

/* ---------- popover del texto seleccionado ---------- */
.ai-pop { position: absolute; z-index: 60; }
.ai-pop[hidden] { display: none; }
.ai-pop-btn {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 6px 11px 6px 9px;
  border: 1px solid var(--accent);
  border-radius: 99px;
  background: var(--accent);
  color: var(--on-accent);
  font: 600 0.84rem var(--font-sans);
  box-shadow: 0 4px 14px rgb(0 0 0 / 0.15);
  cursor: pointer;
}
.ai-pop-btn[hidden] { display: none; }
.ai-pop-row { display: flex; gap: 6px; }
.ai-pop-row[hidden] { display: none; }
.ai-pop-btn.secondary { background: var(--surface); color: var(--text); border-color: var(--border-strong); }
.ai-pop-btn.secondary:hover { border-color: var(--accent); color: var(--accent); }
.ai-pop-form {
  width: min(340px, calc(100vw - 16px));
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  box-shadow: 0 8px 28px rgb(0 0 0 / 0.16);
}
.ai-pop-form[hidden] { display: none; }
.ai-pop-form .ai-field { width: 100%; font-family: var(--font-sans); }
.ai-pop-chips { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 8px; }
.ai-pop-chips button {
  padding: 4px 9px; border-radius: 99px; border: 1px solid var(--border); background: var(--surface-2);
  color: var(--text); font: 0.8rem var(--font-sans); cursor: pointer;
}
.ai-pop-chips button:hover { border-color: var(--accent); color: var(--accent); }
.ai-pop-chips .ghost { background: none; color: var(--text-muted); }
