/* Chat (chat.html, chat.js): the waterfall behind; the message bar alone at its foot, and the window of frosted glass
   that frosts in round it at the first question; the conversation inside (the question, the agent's trace and its
   loader, the answer with its citations and their cards, charts, what to ask next). Newsreader and the font stacks
   are base.css's; the colours are set here, the quiet greys a step darker than usual to read through the glass. */

:root {
  color-scheme: light;
  --accent: #d61a14;
  --bg: #f5f4f0;
  --ink: #1c1c1c;
  --text: #2d2d2d;
  --muted: #44483f;
  --faint: #575b52;
  --rule: rgba(0, 0, 0, 0.09);
  --fill: rgba(0, 0, 0, 0.04);
  --panel: #fff;
  --shine: rgb(255 255 255 / 0.6);
  --ease: cubic-bezier(0.2, 0.7, 0.2, 1);
  /* The conversation's scale: the bar's type over the body's, 18px over 15px. */
  --scale: 1.2;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--sans);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

button,
textarea {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
}

/* A thing that only screen readers say. */
.announce {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* --- The waterfall and the window -------------------------------------------------------------------------------- */

/* The Tibumana piece (shelf/work/falls-tibumana.js) filling the window, faded in over the gorge's own dark green once
   its photograph is ready. */
.bg {
  position: fixed;
  inset: 0;
  z-index: 0;
  overflow: hidden;
  background: #1b2418;
}

.bg .falls-stage {
  position: absolute;
  inset: 0;
  animation: falls-in 1.2s var(--ease) both;
}

@keyframes falls-in {
  from {
    opacity: 0;
  }
}

/* A window of frosted glass floating over the waterfall, which shows through it softened; edge to edge on a phone.
   Before anything is asked it isn't there (only the bar is, a pill of frosted glass of its own at the foot); the
   first question frosts it in round the bar, which keeps its size and place, and the conversation runs the bar's
   full width. No title: it's any chat. */
.app {
  --gap: clamp(0.75rem, 2.5vw, 2rem);
  /* The bar's width, the same before and after: the widest the column can be, less a gutter either side. */
  --bar-w: min(54rem, 100vw - 2 * var(--gap) - 2rem);
  position: fixed;
  inset: var(--gap);
  z-index: 1;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  overflow: hidden;
  border: 1px solid var(--rule);
  border-radius: 14px;
  background: color-mix(in srgb, var(--panel) 55%, transparent);
  -webkit-backdrop-filter: blur(48px) saturate(1.8);
  backdrop-filter: blur(48px) saturate(1.8);
  box-shadow:
    inset 0 1px var(--shine),
    0 1px 2px rgb(0 0 0 / 0.05),
    0 30px 70px -24px rgb(0 0 0 / 0.3);
  transition:
    inset 0.8s var(--ease),
    background-color 0.8s var(--ease),
    border-color 0.8s var(--ease),
    box-shadow 0.8s var(--ease),
    backdrop-filter 0.8s var(--ease),
    -webkit-backdrop-filter 0.8s var(--ease);
}

[data-state="empty"] .app {
  border-color: transparent;
  background-color: transparent;
  -webkit-backdrop-filter: none;
  backdrop-filter: none;
  box-shadow: none;
}

/* New chat, top right, there only with the window. */
.top {
  display: flex;
  justify-content: flex-end;
  padding: 0.9rem 1.25rem;
}

[data-state="empty"] .top {
  visibility: hidden;
}

.new {
  display: grid;
  place-items: center;
  width: 2rem;
  height: 2rem;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: none;
  color: var(--muted);
}

.new:hover {
  color: var(--ink);
  background: var(--fill);
}

.new svg {
  width: 1.1rem;
  height: 1.1rem;
}

/* The thread keeps its column centred whether it scrolls or not, its scrollbar a faint thumb with no track. */
.thread {
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-gutter: stable both-edges;
  scrollbar-color: color-mix(in srgb, var(--ink) 18%, transparent) transparent;
}

/* The conversation is scaled as a whole by --scale, so every piece of it (answer, trace, charts) keeps its place in
   the hierarchy at the bar's size. Zoom scales its width too, so the column is the bar's width divided back down. */
.column {
  zoom: var(--scale);
  max-width: calc(var(--bar-w) / var(--scale));
  margin: 0 auto;
  padding: 1rem 0 2rem;
}

/* --- The message bar --------------------------------------------------------------------------------------------- */

/* One size and place throughout. Alone on the waterfall it's frosted glass of its own; inside the window it's a faint
   lift of the window's glass with a soft shadow, no rim. Its own frost isn't eased: kept on while the window's comes
   in, the two would stack into white, so it goes the moment a question is sent. Clicking in changes nothing about
   it; the caret is enough. The text box carries the bar's height in its own padding, so a click anywhere in the bar
   lands in it. */
.composer {
  position: relative;
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  width: var(--bar-w);
  margin: 0 auto clamp(1.25rem, 6vh, 3.5rem);
  padding-right: 0.5rem;
  border-radius: 2rem;
  background-color: color-mix(in srgb, var(--panel) 16%, transparent);
  box-shadow: 0 10px 28px -14px rgb(0 0 0 / 0.22);
  transition:
    border-radius 0.25s var(--ease),
    background-color 0.8s var(--ease),
    box-shadow 0.8s var(--ease);
}

[data-state="empty"] .composer {
  background-color: color-mix(in srgb, var(--panel) 30%, transparent);
  -webkit-backdrop-filter: blur(22px) saturate(1.6);
  backdrop-filter: blur(22px) saturate(1.6);
  box-shadow: 0 18px 40px -16px rgb(0 0 0 / 0.45);
}

/* Past one line the pill squares off to a rounded panel (chat.js fit). */
.composer[data-multi] {
  border-radius: 1.4rem;
}

/* Past its height the box scrolls, on a faint thumb with no track, so no bright strip shows on glass. */
.composer textarea {
  flex: 1;
  min-height: 1.5em;
  max-height: min(14rem, 40vh);
  padding: 1.15rem 0 1.15rem 1.9rem;
  resize: none;
  border: 0;
  outline: none;
  background: none;
  color: var(--ink);
  font-size: 18px;
  scrollbar-width: thin;
  scrollbar-color: color-mix(in srgb, var(--ink) 22%, transparent) transparent;
}

.composer textarea::placeholder {
  color: color-mix(in srgb, var(--ink) 62%, transparent);
}

/* Send: faint while the box is empty, ready once there's text, and a stop button while an answer plays. */
.send {
  flex: none;
  display: grid;
  place-items: center;
  width: 2.75rem;
  height: 2.75rem;
  margin-bottom: 0.5rem;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: var(--ink);
  color: var(--bg);
}

[data-busy="false"] .composer:has(:placeholder-shown) .send {
  background: var(--fill);
  color: var(--faint);
}

.send:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}

.send svg {
  width: 1.1rem;
  height: 1.1rem;
}

.i-stop,
[data-busy="true"] .i-send {
  display: none;
}

[data-busy="true"] .i-stop {
  display: block;
}

@media (max-width: 600px) {
  .app {
    --bar-w: calc(100vw - 2rem);
  }

  [data-state="thread"] .app {
    inset: 0;
    border: 0;
    border-radius: 0;
    box-shadow: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  .app,
  .composer {
    transition: none;
  }
}

/* --- Messages ---------------------------------------------------------------------------------------------------- */

.turn {
  margin: 1.5rem 0 2.5rem;
}

.msg-user {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 1.25rem;
}

/* The question in a grey bubble on the right, keeping the lines it was typed in, wrapped so no word is left alone on
   a line. A long one folds to its first sentences, as a button that opens it (chat.js bubble). */
.question {
  max-width: min(80%, 34rem);
  margin: 0;
  padding: 0.55rem 0.9rem;
  border: 0;
  border-radius: 14px;
  background: var(--fill);
  color: var(--ink);
  text-align: left;
  white-space: pre-wrap;
  text-wrap: pretty;
}

.pending {
  padding: 0.3rem 0;
}

.prose p {
  margin: 0 0 1rem;
  line-height: 1.65;
}

.prose strong {
  color: var(--ink);
  font-weight: 600;
}

/* An answer stopped mid-sentence ends on a dash. */
.turn.stopped .msg-assistant > .prose:last-child p:last-child::after {
  content: "—";
  color: var(--faint);
}

/* What to ask next: plain lines, darkening under the pointer. Only the last answer offers it. */
.followups {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.4rem;
  margin-top: 1.25rem;
  text-wrap: pretty;
}

.followups button {
  padding: 0;
  border: 0;
  background: none;
  color: var(--muted);
  text-align: left;
}

.followups button:hover {
  color: var(--ink);
}

.turn:not(:last-child) .followups {
  display: none;
}

/* --- The loader -------------------------------------------------------------------------------------------------- */

/* Dots seen through a lens, crossed from the top left by a wave, sized by --ld. A dot rests faint and small and the
   wave takes it --ld-g of the way to full: thinking, every dot rests small and faint and opens full; waiting on a
   tool, the dots of its glyph are held lit, the wave only brightening them, and the rest sink back. Only opacity and
   scale move, between set values (a wave each), so the wave runs on the compositor. Where less motion is asked for,
   it holds its pose and breathes. */
.ld {
  position: relative;
  display: inline-block;
  flex: none;
  width: var(--ld);
  height: var(--ld);
  vertical-align: middle;
  color: var(--ink);
}

.ld i {
  --ld-g: 1;
  position: absolute;
  top: calc(50% - var(--s));
  left: calc(50% - var(--s));
  width: calc(var(--s) * 2);
  height: calc(var(--s) * 2);
  border-radius: 50%;
  background: currentColor;
  transform: scale(0.5);
  opacity: 0.3;
  scale: 0.55;
  animation: ld-wave 2.4s ease-in-out calc(var(--d) * 1.4s - 2.7s) infinite;
}

.ld:is(
    [data-tool="search_passages"],
    [data-tool="list_documents"],
    [data-tool="word_trend"],
    [data-tool="books_trend"],
    [data-tool="get_table"],
    [data-tool="get_election"],
    [data-tool="get_messages"],
    [data-tool="read_document"],
    [data-tool="make_chart"]
  )
  i {
  --ld-g: 0.15;
  opacity: 0.14;
  scale: 0.5;
  animation-name: ld-wave-sunk;
}

/* A magnifier for a search, a line going up for a count over the years, a table, a tick for an election, a page for
   reading, three bars for a chart. */
.ld:is([data-tool="search_passages"], [data-tool="list_documents"]) .m,
.ld:is([data-tool="word_trend"], [data-tool="books_trend"]) .u,
.ld[data-tool="get_table"] .g,
.ld[data-tool="get_election"] .e,
.ld:is([data-tool="get_messages"], [data-tool="read_document"]) .p,
.ld[data-tool="make_chart"] .c {
  --ld-g: 1;
  opacity: 0.8;
  scale: 1;
  animation-name: ld-wave-lit;
}

/* The middle of the lens takes a touch of the accent (--ld-g of it) as the wave crosses it. */
.ld .o::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: color-mix(in srgb, var(--accent) calc(var(--ld-g) * 100%), transparent);
  opacity: 0;
  animation: ld-glow 2.4s ease-in-out calc(var(--d) * 1.4s - 2.7s) infinite;
}

@keyframes ld-wave {
  0%,
  30%,
  to {
    opacity: 0.3;
    scale: 0.55;
  }
  12% {
    opacity: 1;
    scale: 1;
  }
}

@keyframes ld-wave-sunk {
  0%,
  30%,
  to {
    opacity: 0.14;
    scale: 0.5;
  }
  12% {
    opacity: 0.269;
    scale: 0.575;
  }
}

@keyframes ld-wave-lit {
  0%,
  30%,
  to {
    opacity: 0.8;
    scale: 1;
  }
  12% {
    opacity: 1;
    scale: 1;
  }
}

@keyframes ld-glow {
  0%,
  30%,
  to {
    opacity: 0;
  }
  12% {
    opacity: 1;
  }
}

@keyframes ld-rest {
  to {
    opacity: 0.4;
  }
}

@media (prefers-reduced-motion: reduce) {
  .ld {
    animation: ld-rest 1.6s ease-in-out infinite alternate;
  }

  .ld i,
  .ld .o::after {
    animation: none;
  }
}

/* Forced colours paint a background over as Canvas, which would hide the dots: they're left alone and drawn in
   CanvasText, and the accent of the glow is left out. */
@media (forced-colors: active) {
  .ld i {
    forced-color-adjust: none;
    background: CanvasText;
  }

  .ld .o::after {
    display: none;
  }
}

/* --- The trace --------------------------------------------------------------------------------------------------- */

/* A muted 13px line per step, led by a grid of dots that thins with age: the newest has all nine, each older one
   fewer, its dots fading out from the top left as steps arrive. The plan is in the serif's italic. While a step runs
   it's a shade darker, the loader in its glyph's place and a word in its time's; done, what it found is marked after
   its words and its time set at the right. */
.trace {
  margin: 0 0 1.25rem;
  padding: 0;
  list-style: none;
  color: var(--muted);
  font-size: 13px;
  line-height: 24px;
}

.trace li {
  display: flex;
  align-items: baseline;
  gap: 0.6rem;
  animation: tr-enter 0.4s var(--ease) both;
}

.trace .stopped {
  color: var(--faint);
}

.tr-mark {
  flex: none;
  align-self: flex-start;
  display: grid;
  place-items: center;
  width: 12px;
  height: 24px;
}

.tr-text {
  flex: 1;
}

.trace li:not(.done, .stopped) .tr-text {
  color: var(--text);
}

.trace .think .tr-text,
.trace li:not(.done, .stopped) .tr-time {
  font: italic 15px/24px var(--serif);
}

.tr-time {
  color: var(--faint);
  font-variant-numeric: tabular-nums;
  text-align: right;
}

/* The plan has no time to show once done; it keeps the word's room, so its lines don't break anew. */
.trace .think .tr-time {
  min-width: 3.5rem;
}

.tr-dots {
  width: 12px;
  fill: currentColor;
}

.tr-dots circle {
  transition: opacity 0.6s var(--ease) calc(var(--d) * 60ms);
}

.tr-dots .off {
  opacity: 0;
}

/* A glyph's dots open from small and faint as it comes. */
.tr-dots circle:not(.off) {
  transform-box: fill-box;
  transform-origin: center;
  animation: tr-dot 0.5s var(--ease) calc(var(--d) * 40ms) backwards;
}

/* What a step found: a dot a passage (red if Republican, ink if Democratic), a square an election, a grid a table. */
.tr-refs {
  margin-left: 0.5em;
  white-space: nowrap;
}

.tr-refs i {
  display: inline-block;
  width: 5px;
  height: 5px;
  margin-right: 3px;
  border-radius: 50%;
  background: currentColor;
  vertical-align: 1px;
}

.tr-refs .R {
  background: var(--accent);
}

.tr-refs .D {
  background: var(--ink);
}

.tr-refs .election {
  border-radius: 0;
}

/* A table's grid: one dot and three shadows of it. */
.tr-refs .table {
  width: 2px;
  height: 2px;
  margin-right: 6px;
  vertical-align: 4px;
  box-shadow: 3px 0, 0 3px, 3px 3px;
}

.tr-refs span {
  color: var(--faint);
  font-size: 11px;
}

@keyframes tr-enter {
  from {
    opacity: 0;
    translate: 0 4px;
  }
}

@keyframes tr-dot {
  from {
    opacity: 0.35;
    scale: 0.55;
  }
}

@media (prefers-reduced-motion: reduce) {
  .trace :not(.ld, .ld *) {
    animation: none !important;
    transition: none !important;
  }
}

/* --- Citations --------------------------------------------------------------------------------------------------- */

/* A small numbered circle in the line, its target reaching past it. */
.cite {
  position: relative;
  margin-left: 0.25em;
  padding: 0.05em 0.36em;
  border: 1px solid color-mix(in srgb, currentColor 40%, transparent);
  border-radius: 1em;
  color: var(--muted);
  font-size: 0.66em;
  font-variant-numeric: tabular-nums;
  line-height: 0;
  text-decoration: none;
  vertical-align: 0.2em;
}

.cite::after {
  content: "";
  position: absolute;
  inset: -0.7em -0.45em;
}

.cite:hover,
.cite.on {
  border-color: var(--ink);
  background: var(--ink);
  color: var(--bg);
}

/* The card a citation opens, one at a time, under it (or over it) and inside the thread (chat.js place). It lies
   outside the conversation but is drawn at its scale. */
.card {
  --w: min(20rem, calc(var(--r) - 1.5rem));
  position: fixed;
  z-index: 5;
  left: clamp(0.75rem, calc(var(--x) - 1rem), calc(var(--r) - var(--w) - 0.75rem));
  display: grid;
  gap: 0.5rem;
  width: var(--w);
  max-height: calc(var(--h) - 1rem);
  overflow: auto;
  padding: 0.85rem 1rem 0.8rem;
  border: 1px solid var(--rule);
  border-radius: 12px;
  background: var(--panel);
  box-shadow: 0 14px 36px -18px rgba(0, 0, 0, 0.4);
  color: var(--muted);
  font: 12.5px/1.5 var(--sans);
  opacity: 0;
  visibility: hidden;
  transform: translateY(6px);
  zoom: var(--scale);
}

.card.on {
  opacity: 1;
  visibility: visible;
  transform: none;
  transition-delay: 0s;
}

.card > * {
  margin: 0;
}

.card-quote {
  font: 14.5px/1.45 var(--serif);
}

.card-name,
.card-quote mark {
  background: none;
  color: var(--ink);
}

.card-facts {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.1rem 0.9rem;
}

.card-facts dd {
  margin: 0;
  color: var(--text);
}

.card-out {
  color: inherit;
  text-decoration: none;
}

.card-out:hover {
  color: var(--ink);
}

@media (prefers-reduced-motion: no-preference) {
  .card {
    transition:
      opacity 0.15s,
      transform 0.25s var(--ease),
      visibility 0s 0.15s;
  }
}

/* --- Charts ------------------------------------------------------------------------------------------------------ */

/* An etching: dotted grid, hairlines, a point at every value, bars and lone areas hatched (chat.js chart). While the
   plot is .is-drawing it's set down point by point: each mark by its --t (0–1 along x), a line or bar wiped in from
   --t to --u, in --ch-s. */
.chart {
  margin: 1.5rem 0 2rem;
}

.chart figcaption {
  margin-bottom: 0.9rem;
}

.ch-title {
  color: var(--ink);
  font-weight: 600;
}

.ch-sub,
.ch-key,
.ch-source {
  margin: 0.15rem 0 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

.ch-key {
  display: flex;
  flex-wrap: wrap;
  gap: 0 1.1rem;
  margin: -0.4rem 0 0.9rem;
  font-size: 12px;
}

.ch-key svg {
  margin-right: 0.4rem;
  vertical-align: -2px;
}

.ch-source {
  margin-top: 0.8rem;
  font-size: 12px;
}

.ch-plot {
  --ch-s: 1s;
  position: relative;
}

.ch-plot > svg {
  display: block;
  max-width: 100%;
  height: auto;
  overflow: visible;
}

.chart text {
  font: 11px var(--sans);
  fill: var(--muted);
  font-variant-numeric: tabular-nums;
}

.chart :is(.ch-label, .ch-slogan) {
  font-size: 12px;
}

.chart :is(.ch-slogan, .ch-rule-label) {
  fill: var(--ink);
}

.chart :is(.ch-name, .ch-note, .is-won) {
  font-weight: 500;
}

.chart .is-unverified {
  opacity: 0.55;
}

.chart .is-ink {
  --c: var(--ink);
}

.chart .is-red {
  --c: var(--accent);
}

.chart :is(.ch-name, .ch-note),
.ch-mark,
.ch-pt,
.ch-ring {
  fill: var(--c);
}

.ch-grid {
  stroke: var(--faint);
  stroke-dasharray: 1 3;
}

.ch-lane,
.ch-lead {
  stroke: var(--rule);
}

.ch-axis,
.ch-spine,
.ch-xtick,
.ch-stem,
.ch-event-line {
  stroke: var(--faint);
}

.ch-event-line {
  stroke-dasharray: 2 3;
}

.ch-rule {
  stroke: var(--ink);
}

.ch-line {
  fill: none;
  stroke: var(--c);
  stroke-width: 1;
  stroke-linejoin: round;
  stroke-linecap: round;
}

.ch-line.is-est {
  stroke-dasharray: 2 3.5;
  stroke-linecap: butt;
}

.ch-bar {
  fill: var(--ink);
  stroke: var(--ink);
}

.ch-dot,
.ch-bud {
  fill: none;
  stroke: var(--ink);
  stroke-width: 1.25;
}

.ch-dot.is-held,
.ch-bud.is-won {
  fill: var(--ink);
}

.ch-dot.is-lost {
  stroke: var(--accent);
  stroke-width: 1.5;
}

/* A loss, dotted. */
.ch-dot.is-lost,
.ch-bud:not(.is-won) {
  stroke-dasharray: 0 0.1;
  stroke-linecap: round;
}

.chart circle {
  transform-box: fill-box;
  transform-origin: center;
}

.ch-hatch line {
  stroke: var(--c);
  opacity: 0.4;
}

.ch-cursor {
  stroke: var(--muted);
  opacity: 0.6;
}

.ch-ring.is-ring {
  fill: none;
  stroke: var(--ink);
  stroke-width: 1.25;
}

/* The tip the pointer brings up: the values where it is. */
.ch-tip {
  position: absolute;
  max-width: 16rem;
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--rule);
  border-radius: 8px;
  background: var(--panel);
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
}

.ch-tip.is-on {
  opacity: 1;
}

.ch-tip b {
  color: var(--ink);
}

.ch-tip div {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
}

.ch-tip span {
  color: var(--ink);
}

.is-drawing circle {
  animation: ch-pop calc(var(--ch-s) * 0.3) var(--ease) calc(var(--ch-s) * var(--t) * 1.5) both;
}

.is-drawing path {
  animation: ch-wipe calc(var(--ch-s) * (var(--u) - var(--t)) * 1.5) linear calc(var(--ch-s) * var(--t) * 1.5) both;
}

.is-drawing :is(.ch-stem, .ch-lead, text) {
  animation: ch-fade calc(var(--ch-s) * 0.4) var(--ease) calc(var(--ch-s) * (0.1 + var(--t, 0) * 1.5)) both;
}

@keyframes ch-fade {
  from {
    opacity: 0;
  }
}

@keyframes ch-pop {
  from {
    opacity: 0;
    transform: scale(0);
  }
}

@keyframes ch-wipe {
  from {
    clip-path: inset(0 100% 0 0);
  }
  to {
    clip-path: inset(0);
  }
}
