/*
 * Chat tab — the AI writer as margin notes (public/app/studio/chat.js).
 * Lecture Notebook: the assistant writes on the squared page in ink to the
 * inline-end of the red margin rule; the student's messages are small index
 * cards on the inline-end side in violet ink; AI changes are listed as
 * highlighter-marked stamps. Tokens only, logical properties only, both themes
 * come from tokens.css. Motion: stamp press + highlighter sweep on a new change,
 * pen-ink dots while the assistant writes (static under reduced motion).
 *
 * Layout: .chat fills its container as a column (header / scrolling page /
 * composer). If the container is not height-constrained, the page grows and
 * the composer sticks to the bottom of the viewport instead; a studio with a
 * fixed bottom bar can lift it with --chat-sticky-bottom.
 */
.chat {
  --note-indent: 54px;              /* text starts this far from the inline-start edge (rule at 38px) */
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;          /* fills a flex-column host (the studio's .st-chat-host) */
  height: 100%;            /* ...or a host with a fixed height */
  min-height: 0;
  background: var(--paper);
  color: var(--ink);
}

/* ---- header -------------------------------------------------------------- */
.chat-head {
  flex: none;
  display: flex;
  align-items: center;
  gap: var(--gap-3);
  padding: 10px 16px;
  border-block-end: 1px solid var(--line);
  background: var(--paper);
}
.chat-head-mark {
  flex: none;
  display: inline-grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: var(--r-sm);
  background: var(--violet-soft);
  color: var(--violet);
}
.chat-head-mark svg { width: 18px; height: 18px; }
.chat-head-text { flex: 1; min-width: 0; }
.chat-title { font-size: var(--fs-md); font-weight: 600; }
.chat-sub { font-size: var(--fs-xs); color: var(--ink-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-stage { flex: none; }

/* ---- the page ------------------------------------------------------------- */
.chat-body { position: relative; flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; }
.chat-scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  background-attachment: local, local;   /* the squared paper moves with the notes */
}
.chat-scroll:focus-visible { outline-offset: -3px; border-radius: 0; }
.chat-log {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  gap: 20px;
  padding-block: 20px 28px;
  padding-inline: 0 16px;
}

/* ---- one message ------------------------------------------------------------ */
.cm { position: relative; min-width: 0; }
.cm-text { white-space: pre-wrap; overflow-wrap: anywhere; text-align: start; }

/* assistant: ink written on the page, a pen mark in the margin column */
.cm-ai { padding-inline-start: var(--note-indent); }
.cm-ai > .cm-text { font-size: var(--fs-md); line-height: 1.75; color: var(--ink); }
.cm-mark {
  position: absolute;
  inset-inline-start: 10px;
  inset-block-start: 3px;
  display: inline-grid;
  place-items: center;
  width: 20px;
  height: 20px;
  color: var(--ink-3);
}
.cm-mark svg { width: 17px; height: 17px; }

/* student: a small index card on the inline-end side, violet ink */
.cm-me {
  display: flex;
  flex-direction: column;
  align-items: flex-end;           /* the cross axis follows the writing direction: inline-end */
  gap: 6px;
  padding-inline-start: calc(var(--note-indent) + 12px);
}
.cm-card {
  max-width: min(100%, 440px);
  padding: 5px 12px 6px;
  border: 1px solid var(--line);
  border-block-start: 2px solid color-mix(in srgb, var(--margin) 70%, transparent);
  border-radius: 3px 3px var(--r-sm) var(--r-sm);
  background-color: var(--paper);
  /* the card's own faint ruled lines, one per text line */
  background-image: repeating-linear-gradient(to bottom, transparent 0, transparent 23px, var(--grid) 23px, var(--grid) 24px);
  background-origin: content-box;
  box-shadow: var(--shadow-tab);
  color: var(--violet);
}
.cm-card .cm-text { font-size: var(--fs-md); line-height: 24px; }

/* ---- "changed" line: highlighter-marked stamps + Undo / Keep ----------------- */
.cm-changed {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 10px;
  margin-block-start: 12px;
  font-size: var(--fs-sm);
  color: var(--ink-2);
}
.cm-changed-mark { display: inline-grid; color: var(--ink-3); }
.cm-changed-mark svg { width: 17px; height: 17px; }
.cm-stamps { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px 8px; }
.cm-stamps .stamp { max-width: 100%; overflow: hidden; text-overflow: ellipsis; }
.cm-stamps .hl { color: var(--ink); padding-inline: 1px; }   /* ink on the highlighter: AA in both themes */
.cm-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-inline-start: auto; }
.cm-res { margin-inline-start: auto; }
.cm-res:focus { outline: none; }
.cm-res:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* ---- quick replies + starters (chips, full touch targets) -------------------- */
.cm-qr, .chat-starters { display: flex; flex-wrap: wrap; gap: 8px; margin-block-start: 14px; }
.chat .chip {
  min-height: 40px;
  padding: 6px 14px;
  color: var(--ink);
  font-size: var(--fs-sm);
  line-height: 1.35;
  text-align: start;
  transition: border-color var(--t-fast) var(--ease-out), background var(--t-fast) var(--ease-out);
}
.chat .chip:hover { background: var(--violet-soft); }
.chat .chip svg { width: 16px; height: 16px; color: var(--violet); }
.chat .chip[disabled] { opacity: 0.5; pointer-events: none; }
.chat .btn-sm { min-height: 40px; }   /* touch target >= 40px inside the chat */

/* ---- intro (empty conversation) ------------------------------------------------ */
.chat-intro-title { font-size: var(--fs-lg); font-weight: 600; margin-block-end: 6px; }
.chat-intro > .cm-text { color: var(--ink-2); }
.chat-intro-lang { display: flex; align-items: flex-start; gap: 8px; margin-block-start: 12px; font-size: var(--fs-sm); color: var(--ink-3); }
.chat-intro-lang svg { width: 16px; height: 16px; margin-block-start: 3px; }

/* ---- typing: pen-ink dots ----------------------------------------------------------- */
.cm-typing { min-height: 28px; display: flex; align-items: center; }
.ink-dots { display: inline-flex; align-items: center; gap: 5px; height: 24px; }
.ink-dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--ink); opacity: 0.2; animation: ink-dot 1.2s var(--ease-in-out) infinite; }
.ink-dots i:nth-child(2) { animation-delay: 0.18s; }
.ink-dots i:nth-child(3) { animation-delay: 0.36s; }
@keyframes ink-dot {
  0%, 70%, 100% { opacity: 0.2; transform: none; }
  30% { opacity: 0.9; transform: translateY(-2px); }
}

/* ---- errors / failed messages --------------------------------------------------------- */
.cm-error {
  margin-inline-start: var(--note-indent);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  padding: 10px 12px;
  border: 1.5px dashed var(--bad);
  border-radius: var(--r-sm);
  background: var(--bad-soft);
  color: var(--ink);
  font-size: var(--fs-sm);
}
.cm-error > svg { width: 18px; height: 18px; color: var(--bad); flex: none; }
.cm-error p { flex: 1 1 180px; }
.cm-error.is-soft { border-color: var(--line-strong); background: var(--paper-2); }
.cm-error.is-soft > svg { color: var(--ink-3); }
.cm-failed { transform: rotate(-1.5deg); }

/* ---- loading ----------------------------------------------------------------------------- */
.chat-loading { display: grid; gap: 12px; padding-inline-start: var(--note-indent); }
.chat-sk { height: 14px; width: 82%; }
.chat-sk.is-short { width: 56%; }
.chat-sk.is-card { justify-self: end; width: 46%; height: 42px; }

/* ---- "new reply" jump ------------------------------------------------------------------------ */
.chat-jump {
  position: absolute;
  inset-block-end: 12px;
  inset-inline: 0;
  margin-inline: auto;
  width: max-content;
  border-radius: 999px;
  box-shadow: var(--shadow-pop);
  z-index: 3;
}

/* ---- composer ---------------------------------------------------------------------------------- */
.chat-composer {
  flex: none;
  position: sticky;
  inset-block-end: var(--chat-sticky-bottom, 0px);
  z-index: 2;
  padding: 10px 12px calc(8px + env(safe-area-inset-bottom, 0px));
  border-block-start: 1px solid var(--line);
  background: var(--paper);
}
.chat-compose-row { display: flex; align-items: flex-end; gap: 8px; }
.chat-input {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  max-height: 40vh;
  padding-block: 10px;
  resize: none;
  overflow-y: hidden;
  font-size: max(16px, var(--fs-md));   /* 16px: iOS does not zoom the page on focus */
  line-height: 1.5;
}
.chat-input[aria-disabled="true"] { background: var(--paper-2); color: var(--ink-3); cursor: not-allowed; }
.chat-composer.is-paste .chat-input { min-height: 116px; }   /* "paste my old CV": room for the hint + the paste */
.chat-send { flex: none; width: 44px; min-height: 44px; }
.chat-send svg { width: 19px; height: 19px; }
.chat-foot { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 4px 12px; min-height: 18px; margin-block-start: 6px; font-size: var(--fs-xs); color: var(--ink-3); }
.chat-note { margin-inline-start: auto; }
.chat-note.is-warn { color: var(--warn); font-weight: 500; }
@media (pointer: coarse) { .chat-keys { display: none; } }

/* ---- narrow phones (360px) ---------------------------------------------------------------------- */
@media (max-width: 420px) {
  .chat-head { padding-inline: 12px; }
  .chat-sub { display: none; }
  .chat-log { padding-inline-end: 12px; gap: 18px; }
  .cm-me { padding-inline-start: var(--note-indent); }   /* cards line up with the ink column */
  .cm-actions { margin-inline-start: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .ink-dots i { animation: none; opacity: 0.55; }
}
