/* The Chat page (/chat), 2026-11-22. Its stage is a desk: a loose pile of the school's papers on one
   side, the conversation on the other. The question is typed, the pile is searched, the one sheet
   that holds the answer comes out of the pile with the passage marked, and the answer arrives with
   its file and page on a paper tab. On wide screens a red thread ties that tab to its sheet.
   The home page shows answers on white app cards; this page shows the paper itself, so the two
   never look alike. Every step is drawn from [data-step] and [data-q] on .mode-demo (js/modes.js,
   js/mode-chat.js). Sizes follow the stage's own width (cqi), so the desk scales with it. */

/* The three questions to pick from: paper when chosen, like the sheets they are asked of, so they
   never read as the red step rail under the stage. */
.ch-pick { display: flex; flex-wrap: wrap; align-items: center; gap: var(--s-2) var(--s-3); margin-bottom: var(--s-5); }
.ch-pick__l { margin: 0 var(--s-2) 0 0; color: var(--ink-muted); font-weight: 600; }
.ch-pick__q { min-height: 2.75rem; padding: 0.55rem 1.15rem; border: 1px solid var(--rule-firm); border-radius: 1.4rem;
  background: transparent; color: var(--ink); font: inherit; font-weight: 500; line-height: 1.3; text-align: left; cursor: pointer;
  transition: color var(--dur) var(--ease-out-quart), background-color var(--dur) var(--ease-out-quart), border-color var(--dur) var(--ease-out-quart); }
.ch-pick__q:hover { border-color: var(--ink-muted); }
.ch-pick__q[aria-pressed="true"] { border-color: var(--c-paper); background: var(--c-paper); color: var(--c-charcoal); }
.ch-pick__q:focus-visible { outline: 2px solid var(--c-petal); outline-offset: 3px; }

/* The stage measures itself, so everything on the desk can be sized from its width. */
.mode-stage { container: chat / inline-size; }

/* Wide: the pile on the left, the conversation on the right. --cw is a sheet's width, --dx and --dy
   how far each sheet sits from the one under it, --us how much the found sheet grows. */
.ch { --pad: 4.5cqi; --cw: max(28cqi, 230px); --dx: 3cqi; --dy: 4.6cqi; --us: 1.07; --fs-doc: clamp(12px, 1.1cqi, 15px);
  position: relative; display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 0.9fr); align-items: center; gap: 4cqi;
  min-height: inherit; padding: var(--pad); }
.ch p { margin: 0; }

/* The pile. All sheets share one grid cell and are moved into place, so the cell is as tall as the
   tallest sheet and the padding below makes room for the steps down the pile. */
.ch-desk { position: relative; display: grid; justify-self: center; width: calc(var(--cw) + 4 * var(--dx)); padding-bottom: calc(4 * var(--dy)); }
.ch-doc { grid-area: 1 / 1; align-self: start; display: grid; gap: 0.6em; width: var(--cw); padding: 0.7em 0.95em 1.15em; border-radius: 5px;
  background: var(--c-paper); color: var(--c-charcoal); font-size: var(--fs-doc); line-height: 1.4;
  box-shadow: 0 1.2em 2.4em -1.1em oklch(0% 0 0 / 0.8);
  transform: translate(var(--x), var(--y)) rotate(var(--r));
  transition: transform 760ms var(--ease-out-expo), filter 420ms var(--ease-out-quart), box-shadow 760ms var(--ease-out-expo); }
/* Small turns only: any more and a sheet's name hides under the next one. */
.ch-doc:nth-child(1) { --x: 0px; --y: 0px; --r: -1.8deg; --sx: 1.2cqi; --sd: 0ms; }
.ch-doc:nth-child(2) { --x: var(--dx); --y: var(--dy); --r: 1.1deg; --sx: -1cqi; --sd: 70ms; }
.ch-doc:nth-child(3) { --x: calc(2.2 * var(--dx)); --y: calc(2 * var(--dy)); --r: -0.9deg; --sx: 1.1cqi; --sd: 140ms; }
.ch-doc:nth-child(4) { --x: calc(2.9 * var(--dx)); --y: calc(3 * var(--dy)); --r: 1.5deg; --sx: -1.2cqi; --sd: 210ms; }
.ch-doc:nth-child(5) { --x: calc(4 * var(--dx)); --y: calc(4 * var(--dy)); --r: -0.6deg; --sx: 0.9cqi; --sd: 280ms; }

.ch-doc__h { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 0 0.8em; padding-bottom: 0.45em;
  border-bottom: 1px solid oklch(31.8% 0.005 67.7 / 0.14); }
.ch-doc__n { display: inline-flex; align-items: center; gap: 0.45em; font-weight: 700; }
.ch-doc__p { color: var(--c-warmgray); font-weight: 500; }
.ch-doc > i { display: block; height: 0.55em; border-radius: 0.3em; background: oklch(46.4% 0.013 39.4 / 0.2); }
.ch-doc > i:nth-of-type(2n) { width: 82%; }
.ch-doc > i:nth-of-type(3n) { width: 64%; }
.ch-doc__t { font-weight: 600; line-height: 1.45; }

/* The passage's marker: a background that grows across the line. Sliced, not cloned, across line
   breaks, so it runs line after line the way a pen would. */
.ch-mark { background: linear-gradient(oklch(62.9% 0.200 25.3 / 0.34), oklch(62.9% 0.200 25.3 / 0.34)) 0 0 / 0% 100% no-repeat;
  transition: background-size 1000ms var(--ease-out-quart); }

/* The sheet's own red tab mark; it appears when the answer names the sheet, the same mark as on the
   answer's source tab. */
.ch-doc__n::before, .ch-chip__i { content: ""; flex: none; width: 0.7em; height: 0.7em; border-radius: 1px; background: var(--c-red); }
.ch-doc__n::before { transform: scale(0); transition: transform 420ms var(--ease-out-expo); }

/* Ask: the question is typed. js/mode-chat.js lays it out letter by letter, each with its delay in
   --d, so the bubble keeps its size while the letters arrive. */
.ch-chat { display: grid; gap: 1.6em; align-content: center; font-size: clamp(12px, 1.25cqi, 17px); }
.ch-me { justify-self: end; max-width: 90%; padding: 0.7em 1.05em; border: 1px solid var(--rule-firm); border-radius: 1.2em 1.2em 0.3em 1.2em;
  color: var(--ink); font-size: clamp(13px, 1.45cqi, 20px); font-weight: 500; line-height: 1.35;
  transition: background-color var(--dur-slow) var(--ease-out-quart), border-color var(--dur-slow) var(--ease-out-quart); }
.mode-demo:not([data-step="0"]) .ch-me { border-color: transparent; background: oklch(92.7% 0.007 5.6 / 0.09); }
.mode-demo[data-step="0"] .ch-me__t span { animation: ch-type 1ms var(--d, 0ms) both; }
@keyframes ch-type { from { opacity: 0; } }

/* The status line and the three answers share one place, so nothing jumps when one replaces the other. */
.ch-out { display: grid; }
.ch-out > * { grid-area: 1 / 1; }
.ch-status { align-self: start; display: flex; align-items: center; gap: 0.6em; color: var(--ink-faint-text); font-weight: 500;
  opacity: 0; transition: opacity var(--dur) var(--ease-out-quart); }
.ch-status::before { content: ""; width: 0.5em; height: 0.5em; border-radius: 50%; background: var(--c-rose); }
.ch-status__f { display: none; }
.mode-demo:is([data-step="1"], [data-step="2"]) .ch-status { opacity: 1; }
.mode-demo[data-step="2"] .ch-status__s { display: none; }
.mode-demo[data-step="2"] .ch-status__f { display: inline; }
.mode-demo.is-running:is([data-step="1"], [data-step="2"]) .ch-status::before { animation: ch-blink 900ms ease-in-out infinite; }
@keyframes ch-blink { 50% { opacity: 0.25; } }

.ch-a { display: grid; gap: 0.9em; justify-items: start; padding-left: 1em; border-left: 2px solid var(--c-red);
  opacity: 0; visibility: hidden; transform: translateY(0.6em);
  transition: opacity 420ms var(--ease-out-quart), transform 620ms var(--ease-out-expo), visibility 0s linear 620ms; }
.ch-a__t { color: var(--ink); font-size: clamp(15px, 2cqi, 28px); font-weight: 700; line-height: 1.25; letter-spacing: -0.01em; }
.ch-chip { display: inline-flex; align-items: center; gap: 0.55em; padding: 0.4em 0.8em; border-radius: 4px;
  background: var(--c-paper); color: var(--c-charcoal); font-size: clamp(12px, 1.05cqi, 14px); font-weight: 600;
  opacity: 0; transition: opacity 420ms var(--ease-out-quart); }

/* Search: the pile is stirred and a line passes over it. */
.ch-scan { position: absolute; inset: -2cqi; z-index: 4; overflow: hidden; pointer-events: none; opacity: 0; transition: opacity var(--dur) var(--ease-out-quart); }
.ch-scan::before { content: ""; position: absolute; inset: 0; border-bottom: 2px solid var(--c-rose); transform: translateY(-100%);
  background: linear-gradient(to bottom, transparent 75%, oklch(62.9% 0.200 25.3 / 0.16)); }
.mode-demo[data-step="1"] .ch-scan { opacity: 1; }
.mode-demo[data-step="1"] .ch-scan::before { animation: ch-scan 1500ms linear 150ms both; }
@keyframes ch-scan { to { transform: translateY(30%); } }
.mode-demo[data-step="1"] .ch-doc { animation: ch-shuffle 1000ms var(--ease-out-quart) var(--sd) both; }
@keyframes ch-shuffle { 35% { translate: var(--sx) -0.6cqi; } 70% { translate: calc(var(--sx) * -0.5) 0.3cqi; } }

/* Find: the other sheets go dark, the one that holds the answer comes to the top of the pile, turns
   straight, grows a little, and its passage is marked. */
.mode-demo:is([data-step="2"], [data-step="3"]) .ch-doc { filter: brightness(0.42) saturate(0.6); }
.mode-demo:is([data-step="2"], [data-step="3"])[data-q="0"] .ch-doc[data-doc="0"],
.mode-demo:is([data-step="2"], [data-step="3"])[data-q="1"] .ch-doc[data-doc="1"],
.mode-demo:is([data-step="2"], [data-step="3"])[data-q="2"] .ch-doc[data-doc="2"] {
  z-index: 3; filter: none; transform: translate(calc(2 * var(--dx)), calc(1.2 * var(--dy))) rotate(0deg) scale(var(--us));
  box-shadow: 0 2em 3.6em -1.2em oklch(0% 0 0 / 0.9); }
.mode-demo:is([data-step="2"], [data-step="3"])[data-q="0"] .ch-doc[data-doc="0"] .ch-mark,
.mode-demo:is([data-step="2"], [data-step="3"])[data-q="1"] .ch-doc[data-doc="1"] .ch-mark,
.mode-demo:is([data-step="2"], [data-step="3"])[data-q="2"] .ch-doc[data-doc="2"] .ch-mark {
  background-size: 100% 100%; transition-delay: 450ms; }

/* Answer: the answer with its source tab, the sheet's tab mark, and the thread between them. */
.mode-demo[data-step="3"][data-q="0"] .ch-a[data-for="0"],
.mode-demo[data-step="3"][data-q="1"] .ch-a[data-for="1"],
.mode-demo[data-step="3"][data-q="2"] .ch-a[data-for="2"] {
  opacity: 1; visibility: visible; transform: none; transition-delay: 0s; }
.mode-demo[data-step="3"] .ch-a .ch-chip { opacity: 1; transition-delay: 300ms; }
.mode-demo[data-step="3"][data-q="0"] .ch-doc[data-doc="0"] .ch-doc__n::before,
.mode-demo[data-step="3"][data-q="1"] .ch-doc[data-doc="1"] .ch-doc__n::before,
.mode-demo[data-step="3"][data-q="2"] .ch-doc[data-doc="2"] .ch-doc__n::before { transform: scale(1); transition-delay: 300ms; }

/* The thread: placed by js/mode-chat.js from the source tab to the nearest edge of its sheet, then
   drawn out from the tab. */
.ch-link { position: absolute; left: 0; top: 0; z-index: 5; width: var(--len, 0px); height: 2px; background: var(--c-rose); pointer-events: none;
  transform-origin: 0 50%; transform: translate(var(--lx, 0px), var(--ly, 0px)) rotate(var(--la, 0rad)) scaleX(0);
  transition: transform 600ms var(--ease-out-expo); }
.ch-link::after { content: ""; position: absolute; right: -4px; top: -3px; width: 8px; height: 8px; border-radius: 50%; background: var(--c-rose); }
.mode-demo[data-step="3"] .ch-link { transform: translate(var(--lx, 0px), var(--ly, 0px)) rotate(var(--la, 0rad)) scaleX(1); transition-delay: 650ms; }

/* Held, or not yet on screen: everything that moves waits where it is. */
.mode-demo:not(.is-running) .ch-doc, .mode-demo:not(.is-running) .ch-scan::before, .mode-demo:not(.is-running) .ch-me__t span { animation-play-state: paused; }

/* Narrow: the pile above, the conversation under it. The thread would cross the question, so it
   stays off; the matching red marks on tab and sheet carry the link. */
@container chat (max-width: 759px) {
  .ch { --pad: 5cqi; --cw: min(80cqi, 400px); --dx: 1.5cqi; --dy: max(33px, 6cqi); --us: 1.03; --fs-doc: clamp(12px, 2cqi + 5px, 14px);
    grid-template-columns: minmax(0, 1fr); align-content: start; gap: 6cqi; }
  .ch-chat { font-size: clamp(12px, 1.4cqi + 8px, 16px); gap: 1.2em; }
  .ch-me { font-size: clamp(14px, 1.6cqi + 9px, 18px); }
  .ch-a__t { font-size: clamp(16px, 2cqi + 10px, 22px); }
  .ch-chip { font-size: clamp(12px, 1cqi + 8px, 14px); }
  .ch-link { display: none; }
}

/* Reduced motion: the player shows the last step and plays nothing; here nothing moves either, so
   the finished answer, its sheet and its marked passage are simply there. */
@media (prefers-reduced-motion: reduce) {
  .ch *, .ch *::before, .ch *::after { animation: none !important; transition: none !important; }
}
