/* Datum: the site's reference desk, as a floating panel.
 *
 * Every colour, radius and font here comes from tokens.css. Nothing is hardcoded, so
 * the panel follows the site's palette rather than sitting on top of it in its own.
 *
 * The launcher is a real link in the markup, not a JS-created button, so it works
 * before this stylesheet or the script has loaded and still works if the script never
 * arrives. See datum.js for what upgrades it.
 */

/* THIS COMPONENT DECLARES ITS OWN BOX MODEL, because the site does not have one.
 * There is no global `box-sizing: border-box` reset here: measured on production,
 * `main` computes to content-box. So the bottom-sheet rule below, `width: 100%` on an
 * element with a 1px border, produced a panel 322px wide in a 320px viewport, hanging
 * 2px off the right edge. The same arithmetic applies to the desktop `max-height`
 * against `calc(100vh - 3rem)`.
 *
 * Scoped to this component on purpose. A global reset would be the right fix for a new
 * site and the wrong change to make from inside a chat widget, since every existing
 * width and padding on 129 pages was authored against content-box.
 */
.datum,
.datum *,
.datum-launch {
  box-sizing: border-box;
}

/* ---- the launcher ------------------------------------------------------------ */

.datum-launch {
  position: fixed;
  right: clamp(0.75rem, 2vw, 1.5rem);
  bottom: clamp(0.75rem, 2vw, 1.5rem);
  z-index: 60;
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.6rem 1rem;
  border: 1px solid var(--mo-border);
  border-radius: var(--radius-pill);
  background: var(--mo-surface-raised);
  color: var(--mo-text);
  font-family: var(--font-body);
  font-size: 0.9rem;
  line-height: 1;
  text-decoration: none;
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.04), 0 6px 20px rgb(0 0 0 / 0.08);
  cursor: pointer;
  transition:
    transform var(--duration-fast) var(--ease-out),
    border-color var(--duration-fast) var(--ease-out);
}

.datum-launch:hover,
.datum-launch:focus-visible {
  transform: translateY(-1px);
  border-color: var(--mo-accent);
}

.datum-launch[hidden] {
  display: none;
}

/* The mark: a datum symbol, which is what a fixed reference is called on a drawing. */
.datum-launch-mark {
  display: inline-block;
  width: 0.85rem;
  height: 0.85rem;
  flex: none;
  border: 1.5px solid var(--mo-accent);
  border-radius: 50%;
  position: relative;
}

.datum-launch-mark::after {
  content: '';
  position: absolute;
  inset: 3px;
  border-radius: 50%;
  background: var(--mo-accent);
}

/* Announced, never seen. The streaming answer used to sit inside an aria-live region,
 * so every token delta re-announced the whole answer from the top. State changes go
 * here instead, and the answer is read as ordinary content. */
.datum-status {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

/* ---- the panel --------------------------------------------------------------- */

.datum {
  position: fixed;
  right: clamp(0.75rem, 2vw, 1.5rem);
  bottom: clamp(0.75rem, 2vw, 1.5rem);
  z-index: 61;
  display: flex;
  flex-direction: column;
  width: min(26rem, calc(100vw - 1.5rem));
  max-height: min(34rem, calc(100vh - 3rem));
  border: 1px solid var(--mo-border);
  border-radius: var(--radius-lg);
  background: var(--mo-surface);
  color: var(--mo-text);
  font-family: var(--font-body);
  box-shadow: 0 2px 4px rgb(0 0 0 / 0.05), 0 18px 50px rgb(0 0 0 / 0.16);
  overflow: hidden;
}

.datum[hidden] {
  display: none;
}

@media (max-width: 30rem) {
  /* On a phone the panel is the page. A 26rem card floating over content is a
     desktop idea, and at 375px it leaves no room to read the answer. */
  .datum {
    right: 0;
    bottom: 0;
    left: 0;
    width: 100%;
    max-height: 88vh;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    border-bottom: none;
  }
}

.datum-head {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  padding: 0.85rem 1rem;
  border-bottom: 1px solid var(--mo-border);
  background: var(--mo-surface-raised);
}

.datum-name {
  font-family: var(--font-display);
  font-size: 1rem;
  margin: 0;
}

.datum-role {
  font-size: 0.75rem;
  color: var(--mo-muted);
}

.datum-close {
  margin-left: auto;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: none;
  color: var(--mo-muted);
  font-family: var(--font-body);
  font-size: 1.1rem;
  line-height: 1;
  padding: 0.15rem 0.4rem;
  cursor: pointer;
}

.datum-close:hover,
.datum-close:focus-visible {
  color: var(--mo-text);
  border-color: var(--mo-border);
}

@media (pointer: coarse) {
  /* Measured at 320px: the close button came out 26 by 24, which meets the WCAG 2.5.8
     floor by one pixel in one dimension. On the only input where it matters, a thumb,
     one pixel of margin is not a margin. editorial.css already carries a coarse-pointer
     rule for the nav for the same reason. */
  .datum-close {
    min-width: 2rem;
    min-height: 2rem;
  }
}

.datum-log {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  font-size: 0.9rem;
  line-height: var(--leading-body);
}

.datum-intro {
  color: var(--mo-muted);
  font-size: 0.85rem;
  margin: 0;
}

.datum-intro strong {
  color: var(--mo-text);
  font-weight: 500;
}

.datum-seeds {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.datum-seed {
  border: 1px solid var(--mo-border);
  border-radius: var(--radius-pill);
  background: none;
  color: var(--mo-text);
  font-family: var(--font-body);
  font-size: 0.78rem;
  padding: 0.3rem 0.65rem;
  cursor: pointer;
  text-align: left;
}

.datum-seed:hover,
.datum-seed:focus-visible {
  border-color: var(--mo-accent);
}

/* ---- turns ------------------------------------------------------------------- */

.datum-turn {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.datum-turn-who {
  font-size: var(--label-size);
  letter-spacing: var(--label-tracking);
  text-transform: uppercase;
  color: var(--mo-muted);
}

.datum-ask {
  margin: 0;
  padding: 0.5rem 0.7rem;
  border-left: 2px solid var(--mo-accent-soft);
  background: var(--mo-surface-raised);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

.datum-answer > :first-child { margin-top: 0; }
.datum-answer > :last-child { margin-bottom: 0; }
.datum-answer p { margin: 0 0 0.6rem; }
/* `list-style: disc` is stated explicitly, and the selector is deliberately more
   specific than `.datum-answer ul`: editorial.css resets `.ed :where(ul, ol)` to
   `list-style: none` at (0,1,0), and `body.ed` is on every editorial page, so an
   unqualified rule here loses and Datum's bullet lists render as unmarked lines. */
.datum-answer ul,
.datum.datum ul { margin: 0 0 0.6rem; padding-left: 1.1rem; list-style: disc; }
.datum.datum li { list-style: disc; }
.datum-answer li { margin: 0 0 0.25rem; }

.datum-answer a {
  color: var(--mo-text);
  /* `text-decoration` IN FULL, not just its colour. Setting only the colour assumes an
     underline is already there, which editorial.css provides and site.css does not: on
     the six legacy-design pages every citation rendered as plain body text, so the one
     thing that makes an answer checkable was invisible exactly where a reader is most
     likely to be lost. A citation that does not look like a link is not a citation. */
  text-decoration: underline;
  text-decoration-color: var(--mo-accent);
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}

.datum-answer a:hover {
  color: var(--mo-accent-strong);
}

/* ---- sources, shown while the answer is still composing ---------------------- */

.datum-reading {
  font-size: 0.78rem;
  color: var(--mo-muted);
}

.datum-reading-list {
  margin: 0.3rem 0 0;
  padding: 0;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.datum-reading-list a {
  color: var(--mo-muted);
  text-decoration-color: var(--border-subtle);
}

.datum-reading-list a:hover {
  color: var(--mo-text);
}

.datum-working::after {
  content: '';
  display: inline-block;
  width: 0.45rem;
  height: 0.45rem;
  margin-left: 0.35rem;
  border-radius: 50%;
  background: var(--mo-accent);
  animation: datum-pulse 1.1s var(--ease-out) infinite;
}

@keyframes datum-pulse {
  0%, 100% { opacity: 0.25; }
  50% { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .datum-launch,
  .datum-working::after {
    transition: none;
    animation: none;
  }
}

.datum-error {
  color: var(--mo-error);
  font-size: 0.85rem;
  margin: 0;
}

/* ---- the composer ------------------------------------------------------------ */

.datum-form {
  display: flex;
  gap: 0.5rem;
  align-items: flex-end;
  padding: 0.75rem 1rem 0.85rem;
  border-top: 1px solid var(--mo-border);
  background: var(--mo-surface-raised);
}

.datum-input {
  flex: 1;
  resize: none;
  min-height: 2.4rem;
  max-height: 7rem;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--mo-border);
  border-radius: var(--radius-sm);
  background: var(--mo-surface);
  color: var(--mo-text);
  font-family: var(--font-body);
  font-size: 0.9rem;
  line-height: 1.4;
}

.datum-input:focus-visible {
  outline: 2px solid var(--mo-accent);
  outline-offset: 1px;
}

.datum-send {
  flex: none;
  padding: 0.5rem 0.85rem;
  border: 1px solid var(--mo-accent);
  border-radius: var(--radius-sm);
  background: var(--mo-accent);
  /* NOT --mo-white: on this accent that measures about 2.07:1, well under the 4.5:1
     floor for button text. The site's own convention for type on the accent is the
     near-black ink, which is both compliant and consistent. */
  color: var(--mo-black);
  font-family: var(--font-body);
  font-size: 0.85rem;
  cursor: pointer;
}

.datum-send:disabled {
  opacity: 0.5;
  cursor: default;
}

.datum-foot {
  padding: 0 1rem 0.75rem;
  margin: 0;
  font-size: 0.7rem;
  color: var(--mo-muted);
  background: var(--mo-surface-raised);
}
