/* Assistant chat widget for aischool.fmin.xyz. Class prefix `ais-` so nothing
   collides with the Next app's own styles. Palette mirrors app/globals.css
   (paper/ink/line/accent), so the sheet reads as part of the textbook. */

:root {
  --ais-bg: #fffef9;
  --ais-fg: #171915;
  --ais-muted: #696d65;
  --ais-accent: #171915;
  --ais-accent-fg: #fffef9;
  --ais-border: #deddd4;
  --ais-card: #f5f3ea;
  --ais-user-bg: #171915;
  --ais-user-fg: #fffef9;
  --ais-assistant-bg: #f5f3ea;
  --ais-link: #315f8c;
  --ais-radius: 10px;
  --ais-shadow: 0 8px 30px rgba(23,25,21,.14), 0 2px 6px rgba(23,25,21,.06);
}

/* The widget toggles visibility with the `hidden` attribute. On sigma the
   Bootstrap reset (`[hidden]{display:none!important}`) did that job; this app
   has no such reset, and our own `display:flex` rules would win over the UA
   default — so state it explicitly, or the invisible sheet swallows clicks. */
.ais-launcher[hidden],
.ais-sheet[hidden],
.ais-fragment-pill[hidden],
.ais-fragment-chip[hidden],
.ais-images[hidden],
.ais-trace[hidden] { display: none !important; }

/* Launcher pill (bottom-right) */
.ais-launcher {
  position: fixed; bottom: 16px; right: 16px; z-index: 9998;
  display: inline-flex; align-items: center; gap: 8px;
  padding: 10px 16px; border-radius: 999px;
  background: var(--ais-accent); color: var(--ais-accent-fg);
  border: none; box-shadow: var(--ais-shadow);
  font: 600 14px/1 var(--font-ui, ui-sans-serif), system-ui, sans-serif;
  cursor: pointer; transition: transform .15s;
}
.ais-launcher:hover { transform: translateY(-1px); }
.ais-launcher-icon { font-size: 16px; }
@media (max-width: 640px) {
  .ais-launcher { left: 50%; right: auto; transform: translateX(-50%); bottom: calc(12px + env(safe-area-inset-bottom)); }
  .ais-launcher:hover { transform: translateX(-50%) translateY(-1px); }
}

/* Tiny pill that follows a text selection */
.ais-fragment-pill {
  position: fixed; z-index: 9999;
  padding: 6px 12px; border-radius: 999px;
  background: var(--ais-accent); color: var(--ais-accent-fg);
  border: none; box-shadow: var(--ais-shadow);
  font: 500 12px/1 var(--font-ui, ui-sans-serif), system-ui, sans-serif;
  cursor: pointer;
}

/* Sheet (bottom-sheet on mobile, centered card on desktop) */
.ais-sheet {
  position: fixed; z-index: 9999;
  background: var(--ais-bg); color: var(--ais-fg);
  border: 1px solid var(--ais-border); border-radius: var(--ais-radius);
  box-shadow: var(--ais-shadow);
  display: flex; flex-direction: column;
  /* desktop default */
  right: 16px; bottom: 16px; width: 460px; max-width: calc(100vw - 32px);
  height: 640px; max-height: calc(100vh - 32px);
}
@media (max-width: 640px) {
  .ais-sheet {
    left: 0; right: 0; bottom: 0; top: auto;
    width: 100vw; max-width: 100vw; border-radius: 16px 16px 0 0;
    height: 80vh; max-height: 90vh;
    padding-bottom: env(safe-area-inset-bottom);
  }
}
body.ais-sheet-open { overflow: hidden; }

.ais-sheet-header {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 14px; border-bottom: 1px solid var(--ais-border);
  flex-shrink: 0;
}
.ais-sheet-title { font: 600 14px/1.2 var(--font-ui, ui-sans-serif), system-ui, sans-serif; }
.ais-sheet-model {
  font: 400 11px/1 ui-monospace, monospace; color: var(--ais-muted);
  margin-left: auto; padding: 3px 8px; border-radius: 6px; background: var(--ais-card);
  max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.ais-sheet-close {
  background: transparent; border: none; cursor: pointer;
  font-size: 22px; line-height: 1; color: var(--ais-muted);
  width: 28px; height: 28px; border-radius: 8px;
}
.ais-sheet-close:hover { background: var(--ais-card); color: var(--ais-fg); }

.ais-sheet-body {
  flex: 1; overflow-y: auto; padding: 14px;
  display: flex; flex-direction: column; gap: 12px;
}
.ais-empty {
  font: 13px/1.5 var(--font-ui, ui-sans-serif), system-ui, sans-serif;
  color: var(--ais-muted);
  padding: 16px; background: var(--ais-card); border-radius: 10px;
  border: 1px dashed var(--ais-border);
}

/* Bubbles */
.ais-bubble {
  max-width: 92%;
  border-radius: 12px; padding: 10px 12px;
  font: 14px/1.5 var(--font-ui, ui-sans-serif), system-ui, sans-serif;
  word-wrap: break-word;
}
.ais-bubble-user {
  align-self: flex-end;
  background: var(--ais-user-bg); color: var(--ais-user-fg);
}
.ais-bubble-assistant {
  align-self: flex-start;
  background: var(--ais-assistant-bg); color: var(--ais-fg);
  border: 1px solid var(--ais-border);
}
.ais-bubble-fragment {
  font-size: 12px; color: var(--ais-user-fg); opacity: .75;
  font-style: italic; margin-bottom: 6px;
  padding-bottom: 6px; border-bottom: 1px dashed currentColor;
}
.ais-bubble-text { white-space: pre-wrap; }

.ais-status {
  font-size: 12px; color: var(--ais-muted);
  display: flex; align-items: center; gap: 6px;
  margin-bottom: 6px;
}
.ais-status::before {
  content: ""; width: 8px; height: 8px; border-radius: 50%;
  background: var(--ais-muted); animation: ais-pulse 1.2s infinite;
}
@keyframes ais-pulse {
  0%,100% { opacity: .3; } 50% { opacity: 1; }
}

/* Trace — minimalist tree (vertical trunk + nodes with monochrome SVG icons) */
.ais-trace {
  margin: 4px 0; font: 12px/1.4 var(--font-ui, ui-sans-serif), system-ui, sans-serif;
}
.ais-trace-body {
  position: relative;
  display: flex; flex-direction: column; gap: 0;
  padding: 2px 4px 2px 18px;
}
.ais-trace-body::before {
  content: ""; position: absolute;
  left: 9px; top: 10px; bottom: 10px; width: 1px;
  background: var(--ais-border);
}
.ais-trace-collapsed .ais-trace-body { display: none; }

/* Summary pill (collapsed) */
.ais-trace-summary {
  display: none;
  align-items: center; gap: 8px;
  width: 100%; box-sizing: border-box;
  background: var(--ais-card); border: 1px solid var(--ais-border);
  border-radius: 8px; padding: 5px 10px;
  cursor: pointer; color: var(--ais-fg);
  font: 12px/1 var(--font-ui, ui-sans-serif), system-ui, sans-serif;
  transition: background .12s ease;
}
.ais-trace-summary:not([hidden]) { display: inline-flex; }
.ais-trace-summary:hover { background: var(--ais-border); }
.ais-trace-summary-icons { display: inline-flex; gap: 4px; }
.ais-trace-summary-icons .ais-trace-icon { width: 13px; height: 13px; color: var(--ais-fg); }
.ais-trace-summary-icons svg { width: 13px; height: 13px; display: block; }
.ais-trace-summary-count {
  color: var(--ais-muted); font-size: 11px; flex: 1;
  text-align: left;
}
.ais-trace-caret {
  width: 12px; height: 12px; color: var(--ais-muted);
  transition: transform .15s ease;
}
.ais-trace-collapsed .ais-trace-caret { transform: rotate(-90deg); }
.ais-trace-node {
  position: relative;
  display: flex; flex-direction: column;
  padding: 4px 0; min-width: 0;
}
.ais-trace-node::before {
  /* horizontal branch tick — aligned with row center (collapsed state) */
  content: ""; position: absolute; left: -9px; top: 13px;
  width: 7px; height: 1px; background: var(--ais-border);
}
.ais-trace-row {
  display: flex; align-items: center; gap: 8px; min-width: 0;
  cursor: pointer;
  border-radius: 5px;
  padding: 2px 4px;
  margin: 0 -4px;
  transition: background-color .12s ease;
}
.ais-trace-row:hover { background: var(--ais-card); }
.ais-trace-chev {
  flex-shrink: 0; width: 12px; height: 12px;
  color: var(--ais-muted); display: inline-flex;
  align-items: center; justify-content: center;
  transition: transform .15s ease;
}
.ais-trace-chev svg { width: 12px; height: 12px; display: block; }
.ais-trace-expanded > .ais-trace-row .ais-trace-chev {
  transform: rotate(90deg);
}
.ais-trace-details {
  display: none;
  margin: 6px 0 4px 22px;
  border-left: 2px solid var(--ais-border);
  padding-left: 10px;
  flex-direction: column;
  gap: 8px;
}
.ais-trace-expanded > .ais-trace-details { display: flex; }
.ais-trace-detail-block { display: flex; flex-direction: column; gap: 3px; }
.ais-trace-detail-h {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: .05em;
  font-weight: 600;
  color: var(--ais-muted);
}
.ais-trace-detail-code {
  margin: 0;
  font: 11.5px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  background: var(--ais-card);
  color: var(--ais-fg);
  padding: 7px 9px;
  border-radius: 6px;
  border: 1px solid var(--ais-border);
  max-height: 240px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
}
.ais-trace-icon {
  flex-shrink: 0; width: 14px; height: 14px;
  color: var(--ais-fg); display: inline-flex;
  align-items: center; justify-content: center;
}
.ais-trace-icon svg { width: 14px; height: 14px; display: block; }
.ais-trace-label {
  font-weight: 600; color: var(--ais-fg); flex-shrink: 0;
}
.ais-trace-arg {
  color: var(--ais-muted); font-style: italic; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1;
}
.ais-trace-status {
  flex-shrink: 0; display: inline-flex; gap: 5px; align-items: center;
  color: var(--ais-muted); font-size: 11px;
}
.ais-trace-status svg { width: 13px; height: 13px; display: block; }
.ais-trace-count {
  background: var(--ais-card); padding: 1px 6px; border-radius: 4px;
  font: 500 10px/1.4 ui-monospace, monospace;
}
.ais-trace-ok .ais-trace-status { color: var(--ais-muted); opacity: .85; }
.ais-trace-err .ais-trace-status { color: var(--ais-muted); opacity: .9; }
.ais-trace-pending .ais-trace-status { color: var(--ais-muted); }

.ais-spin {
  transform-origin: center; animation: ais-spin 0.9s linear infinite;
}
@keyframes ais-spin { to { transform: rotate(360deg); } }

/* Images / figures from python */
.ais-images { display: flex; flex-direction: column; gap: 8px; margin: 6px 0; }
.ais-figure {
  max-width: 100%; height: auto; border-radius: 8px;
  border: 1px solid var(--ais-border); background: white;
}
/* Inline ![alt](url) image embedded by the model (book figures, http, data:). */
.ais-figref { margin: 8px 0; display: flex; flex-direction: column; gap: 4px; }
.ais-figcap {
  font-size: 12.5px; color: var(--ais-muted, #6b7280); line-height: 1.4;
}
/* When a figure ref couldn't resolve to a real image, the caption stands alone. */
span.ais-figcap { display: inline-block; }

/* Markdown rendering */
.ais-answer p { margin: 0 0 8px 0; }
.ais-answer p:last-child { margin-bottom: 0; }
.ais-answer h3, .ais-answer h4, .ais-answer h5, .ais-answer h6 {
  margin: 12px 0 6px; line-height: 1.25; font-weight: 700;
}
.ais-answer h3 { font-size: 15px; }
.ais-answer h4 { font-size: 14px; }
.ais-answer h5, .ais-answer h6 { font-size: 13px; color: var(--ais-muted); }
.ais-answer h3:first-child, .ais-answer h4:first-child { margin-top: 0; }
.ais-answer ul, .ais-answer ol { margin: 4px 0 8px; padding-left: 22px; }
.ais-answer ul li, .ais-answer ol li { margin: 2px 0; }
.ais-answer hr {
  border: none; border-top: 1px solid var(--ais-border); margin: 10px 0;
}
.ais-answer blockquote {
  border-left: 3px solid var(--ais-border);
  margin: 6px 0; padding: 2px 10px; color: var(--ais-muted);
}
.ais-answer a.ais-link {
  color: var(--ais-fg); text-decoration: underline; text-decoration-thickness: 1px;
  text-underline-offset: 2px; text-decoration-color: var(--ais-muted);
  font-weight: 500;
}
.ais-answer a.ais-link:hover {
  text-decoration-color: var(--ais-fg);
}
.ais-answer code {
  background: var(--ais-card); padding: 1px 5px; border-radius: 4px;
  font: 13px/1.3 ui-monospace, monospace;
}
.ais-answer pre.ais-code {
  background: var(--ais-card); padding: 10px 12px; border-radius: 8px;
  overflow-x: auto; font: 12px/1.4 ui-monospace, monospace; margin: 6px 0;
}
.ais-answer pre.ais-code code {
  background: transparent; padding: 0; border-radius: 0;
}
.ais-answer .katex-display {
  overflow-x: auto; padding: 4px 0; margin: 6px 0;
}
.ais-answer table.ais-tbl {
  border-collapse: collapse; font-size: 13px; margin: 8px 0;
  width: 100%; overflow-x: auto; display: block;
}
.ais-answer table.ais-tbl th,
.ais-answer table.ais-tbl td {
  border: 1px solid var(--ais-border); padding: 4px 8px; text-align: left;
}
.ais-answer table.ais-tbl th { background: var(--ais-card); font-weight: 600; }

/* Footer (fragment chip + suggestions + input) */
.ais-sheet-footer {
  flex-shrink: 0; padding: 10px 14px 14px;
  border-top: 1px solid var(--ais-border);
  display: flex; flex-direction: column; gap: 8px;
}
.ais-fragment-chip {
  display: flex; gap: 6px; align-items: center;
  padding: 6px 10px; background: var(--ais-card); border-radius: 8px;
  font: 12px/1.3 var(--font-ui, ui-sans-serif), system-ui, sans-serif; color: var(--ais-muted);
}
.ais-fragment-text { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ais-fragment-clear {
  background: transparent; border: none; cursor: pointer;
  color: var(--ais-muted); font-size: 16px; line-height: 1;
  width: 20px; height: 20px; border-radius: 4px;
}
.ais-fragment-clear:hover { background: var(--ais-border); color: var(--ais-fg); }

.ais-suggestions { display: flex; gap: 6px; flex-wrap: wrap; }
.ais-suggest {
  border: 1px solid var(--ais-border); background: transparent;
  color: var(--ais-fg); cursor: pointer;
  padding: 5px 10px; border-radius: 999px;
  font: 12px/1 var(--font-ui, ui-sans-serif), system-ui, sans-serif;
}
.ais-suggest:hover { background: var(--ais-card); }

.ais-inputrow {
  display: flex; gap: 6px; align-items: flex-end;
}
.ais-input {
  flex: 1; resize: none; border: 1px solid var(--ais-border); border-radius: 10px;
  padding: 9px 12px; font: 14px/1.4 var(--font-ui, ui-sans-serif), system-ui, sans-serif;
  background: var(--ais-bg); color: var(--ais-fg);
  min-height: 38px; max-height: 120px;
}
.ais-input:focus { outline: 2px solid var(--ais-accent); outline-offset: -1px; }
.ais-send, .ais-reset {
  border: none; border-radius: 10px; cursor: pointer;
  width: 38px; height: 38px; font-size: 18px; line-height: 1;
  display: inline-flex; align-items: center; justify-content: center;
}
.ais-send {
  background: var(--ais-accent); color: var(--ais-accent-fg);
}
.ais-send:disabled { opacity: .4; cursor: not-allowed; }
.ais-reset {
  background: transparent; color: var(--ais-muted);
  border: 1px solid var(--ais-border);
}
.ais-reset:hover { background: var(--ais-card); color: var(--ais-fg); }

.ais-disclaimer {
  font: 10px/1.3 var(--font-ui, ui-sans-serif), system-ui, sans-serif;
  color: var(--ais-muted); text-align: center;
}

/* Inline algorithm runner (under .callout-important blocks) */
.ais-algo-runner {
  margin: -0.5rem 0 1.25rem;
  padding: 0.85rem 1rem;
  background: var(--ais-card);
  border: 1px solid var(--ais-border);
  border-top: none;
  border-radius: 0 0 8px 8px;
  font: 14px/1.45 var(--font-ui, ui-sans-serif), system-ui, sans-serif;
}
.ais-algo-form {
  display: flex; flex-wrap: wrap; gap: 0.5rem 0.75rem; align-items: end;
  margin: 0;
}
.ais-algo-label {
  display: flex; flex-direction: column; gap: 0.2rem;
  font-size: 12px; color: var(--ais-muted);
}
.ais-algo-input {
  width: 6.5rem; padding: 0.35rem 0.5rem;
  border: 1px solid var(--ais-border); border-radius: 6px;
  background: var(--ais-bg); color: var(--ais-fg);
  font: 13px/1 ui-monospace, "SF Mono", Menlo, monospace;
}
.ais-algo-input:focus {
  outline: 2px solid var(--ais-accent); outline-offset: -1px;
}
select.ais-algo-input {
  width: auto; min-width: 6.5rem; padding-right: 1.5rem; cursor: pointer;
}
.ais-algo-run, .ais-algo-reset {
  height: 32px; padding: 0 0.85rem; border-radius: 6px; cursor: pointer;
  font: 600 13px/1 var(--font-ui, ui-sans-serif), system-ui, sans-serif;
}
.ais-algo-run {
  background: var(--ais-accent);
  color: var(--ais-accent-fg); border: 1px solid transparent;
}
.ais-algo-run:disabled { opacity: .5; cursor: progress; }
.ais-algo-reset {
  background: transparent; color: var(--ais-muted);
  border: 1px solid var(--ais-border);
}
.ais-algo-reset:hover {
  background: var(--ais-bg); color: var(--ais-fg);
}
.ais-algo-out {
  margin-top: 0.85rem; padding-top: 0.75rem;
  border-top: 1px dashed var(--ais-border);
}
.ais-algo-status {
  font-size: 12px; color: var(--ais-muted);
  margin-bottom: 0.4rem;
  display: flex; align-items: center; gap: 0.45rem;
}
.ais-algo-spinner {
  flex: 0 0 auto;
  width: 13px; height: 13px;
  border: 2px solid var(--ais-border);
  border-top-color: var(--ais-accent);
  border-radius: 50%;
  animation: ais-algo-spin 0.7s linear infinite;
}
@keyframes ais-algo-spin { to { transform: rotate(360deg); } }
/* Скелетон-плейсхолдер на месте будущего графика, пока греется Python. */
.ais-algo-skeleton {
  height: 180px; margin: 0.4rem 0 0.2rem;
  border-radius: 8px;
  background: linear-gradient(
    100deg,
    var(--ais-card) 30%,
    var(--ais-border) 50%,
    var(--ais-card) 70%
  );
  background-size: 220% 100%;
  animation: ais-algo-shimmer 1.3s ease-in-out infinite;
}
@keyframes ais-algo-shimmer {
  from { background-position: 180% 0; }
  to   { background-position: -20% 0; }
}
@media (prefers-reduced-motion: reduce) {
  .ais-algo-spinner, .ais-algo-skeleton { animation: none; }
}
.ais-algo-stdout {
  margin: 0; padding: 0.65rem 0.8rem;
  background: var(--ais-bg);
  border: 1px solid var(--ais-border); border-radius: 6px;
  font: 12.5px/1.5 ui-monospace, "SF Mono", Menlo, monospace;
  color: var(--ais-fg);
  white-space: pre-wrap; word-wrap: break-word;
  overflow-x: auto; max-height: 24rem;
}
.ais-algo-stdout[data-has-err="1"] {
  border-color: #d96a6a; background: #fff5f5; color: #7a2222;
}
.ais-algo-images { margin-top: 0.5rem; display: grid; gap: 0.4rem; }
.ais-algo-images img { max-width: 100%; border-radius: 6px; }
@media (prefers-color-scheme: dark) {
  .ais-algo-stdout[data-has-err="1"] { background: #341515; color: #ffb4b4; }
}

/* Native learning-action chips (🎯 Проверь меня, 🧠 Глубокий вопрос) — bright,
   accent-tinted, distinct from neutral suggestions without crowding the row. */
.ais-suggest-accent {
  border-color: color-mix(in srgb, var(--ais-accent) 45%, var(--ais-border));
  background: color-mix(in srgb, var(--ais-accent) 9%, transparent);
  color: var(--ais-accent);
  font-weight: 600;
}
.ais-suggest-accent:hover {
  background: var(--ais-accent);
  color: var(--ais-accent-fg, #fff);
  border-color: var(--ais-accent);
}

/* Quiz answer spoiler: ||...|| renders hidden (blurred) until clicked. */
.ais-spoiler {
  background: color-mix(in srgb, var(--ais-accent) 12%, var(--ais-card));
  color: transparent;
  border-radius: 5px;
  padding: 0 4px;
  cursor: pointer;
  filter: blur(4px);
  user-select: none;
  transition: filter .15s ease, color .15s ease, background .15s ease;
  box-decoration-break: clone;
}
.ais-spoiler::after { content: "  показать"; color: var(--ais-accent); filter: none; font-size: .82em; }
.ais-spoiler.revealed {
  color: var(--ais-fg);
  background: transparent;
  filter: none;
  cursor: default;
  user-select: text;
}
.ais-spoiler.revealed::after { content: ""; }
