/*
 * Smartie — critical path.
 *
 * Everything needed to paint the widget's first frame: the greeting, the composer
 * and the dock. Message styling, citations, the scroll button and motion live in
 * chat.noncritical.css, because none of them are on screen at first paint.
 *
 * No literal colours or fonts here — see chat.design.css.
 */

:host {
  /* The widget owns its own box entirely; nothing leaks in from the host page. */
  all: initial;
  box-sizing: border-box;

  /* The chat fills whatever the host page gives it. */
  display: block;
  block-size: 100%;
  font-family: var(--smartie-font-body);
  font-size: var(--smartie-size-base);
  line-height: var(--smartie-leading-body);
  color: var(--smartie-color-ink);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

:host([hidden]) {
  display: none;
}

*,
*::before,
*::after {
  box-sizing: inherit;
}

/* Class rules below set `display`, which would otherwise beat the user agent's
 * rule for [hidden]. */
[hidden] {
  display: none !important;
}

/* ---------------------------------------------------------------------------
 * Shared controls
 * ------------------------------------------------------------------------ */

.button {
  appearance: none;
  margin: 0;
  border: none;
  background: none;
  font: inherit;
  color: inherit;
  cursor: pointer;

  &:focus-visible {
    outline: var(--smartie-focus-width) solid var(--smartie-color-focus);
    outline-offset: var(--smartie-focus-offset);
  }
}

.visually-hidden {
  position: absolute;
  inline-size: 0.0625rem;
  block-size: 0.0625rem;
  padding: 0;
  margin: -0.0625rem;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

/* ---------------------------------------------------------------------------
 * Panel shell
 *
 * The panel is the page: no border, no card, and its content lines up with the
 * site container so the chat sits under the header rather than beside it.
 * ------------------------------------------------------------------------ */

.panel {
  position: relative;
  container-type: inline-size;
  container-name: smartie-panel;
  display: flex;
  flex-direction: column;
  block-size: 100%;
  min-block-size: 0;
  overflow: hidden;
  background-color: var(--smartie-color-paper);
}

/* One rule holds the whole chat to the site's container. Everything that is a
 * band across the panel is full width; everything inside one of those bands is
 * capped and centred. The log's announcer is not a band: it is out of flow and
 * visually hidden, so it is not listed here. */
.panel__head,
.dock__inner,
.intro,
.log__turns,
.log__status,
.log__alert {
  inline-size: 100%;
  max-inline-size: var(--smartie-shell-width);
  margin-inline: auto;
}

/* ---------------------------------------------------------------------------
 * Head — the name and the way out. Hidden until there is a conversation: the
 * greeting introduces Smartie perfectly well on its own.
 * ------------------------------------------------------------------------ */

.panel__head {
  flex: none;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--smartie-space-md);
  padding-inline: var(--smartie-shell-gutter);
  padding-block: 0 var(--smartie-space-sm);
}

.brand {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.125rem;
  margin: var(--smartie-space-lg) 0 0;
}

.brand__mark {
  display: block;
  inline-size: var(--smartie-wordmark-width);
  block-size: auto;
}

.brand__tag {
  font-size: var(--smartie-size-tag);
  font-weight: var(--smartie-weight-medium);
  letter-spacing: var(--smartie-tracking-tag);
  line-height: 1;
  text-transform: uppercase;
  color: var(--smartie-color-ink-faint);
}

/* The glyph is small, so the box around it is pulled out to the container edge
 * and up to the top of the panel. What the pointer gets is still 44px. */
.panel__new-chat {
  display: grid;
  place-items: center;
  flex: none;
  inline-size: var(--smartie-target-min);
  block-size: var(--smartie-target-min);
  margin-inline-end: calc(var(--smartie-space-md) * -1);
  border-radius: var(--smartie-radius-pill);
  color: var(--smartie-color-brand);

  /* 12px, which is what the design draws. */
  & svg {
    inline-size: 0.75rem;
    block-size: 0.75rem;
    fill: none;
    stroke: currentColor;
    stroke-width: 3.5;
    stroke-linecap: round;
  }
}

/* ---------------------------------------------------------------------------
 * Log
 * ------------------------------------------------------------------------ */

.log {
  position: relative;
  flex: 1 1 auto;
  min-block-size: 0;

  overflow-y: auto;
  scroll-padding-block-start: var(--smartie-space-2xl);
  padding-inline: var(--smartie-shell-gutter);
  padding-block: var(--smartie-space-md) var(--smartie-log-tail);
  background-color: var(--smartie-color-paper);
  display: flex;
  flex-direction: column;
}

/* The greeting is a screenful on its own, so it sits in the middle of what is
 * left rather than at the top of it. Auto margins rather than justify-content,
 * because a centred flex item taller than its scroller overflows off the top
 * where nothing can scroll back to it — which is exactly what a phone keyboard
 * does to this box. */
.log--empty .intro {
  margin-block: auto;
}

/*
 * The transcript, the pending status, the failure alert and the announcer are
 * four separate regions so they announce independently. The announcer is
 * visually hidden and out of flow, so it takes no space here.
 *
 * Spacing is deliberately margin rather than a flex gap on .log. A gap would
 * reserve space around the two regions while they are empty, and hiding them
 * with :empty { display: none } is not an option: a live region that is
 * display:none when content lands is a live region screen readers may never
 * announce. Margin on :not(:empty) keeps all three in the accessibility tree
 * at all times.
 */
.log__turns {
  display: flex;
  flex-direction: column;
  gap: var(--smartie-space-2xs);
}

/* The dots stand in for the answer that is coming, so they sit where it will,
 * one tight step under the question. A failure is a break in the conversation
 * and gets the wider gap. */
.log__status:not(:empty) {
  margin-block-start: var(--smartie-space-2xs);
}

.log__alert:not(:empty) {
  margin-block-start: var(--smartie-space-lg);
}

/* ---------------------------------------------------------------------------
 * Greeting
 *
 * One column on a phone with the robot on top, two columns from tablet up with
 * the robot beside the copy. It is second in the markup either way, because it
 * is decoration and the heading is the thing worth reaching first.
 * ------------------------------------------------------------------------ */

.intro {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--smartie-space-lg);
  text-align: center;
}

.intro__figure {
  flex: none;
}

.intro__robot img {
  display: block;
  inline-size: var(--smartie-figure-width);
  block-size: auto;
}

.intro__copy {
  display: flex;
  flex-direction: column;
  align-items: center;
  inline-size: 100%;
}

.intro__title {
  margin: 0;
  font-family: var(--smartie-font-display);
  font-size: var(--smartie-size-display);
  font-weight: var(--smartie-weight-bold);
  line-height: var(--smartie-leading-tight);
  color: var(--smartie-color-brand-deep);
  text-wrap: balance;
}

/* The logotype finishes the sentence, so it is sized off the heading rather
 * than off a fixed pixel width. */
.intro__wordmark {
  display: inline-block;
  block-size: 0.88em;
  inline-size: auto;
  vertical-align: baseline;
}

.intro__lede {
  margin: var(--smartie-space-xs) 0 0;
  font-size: var(--smartie-size-lg);
  line-height: var(--smartie-leading-tight);
}

.intro__composer {
  inline-size: 100%;
  margin-block-start: var(--smartie-space-xl);
}

.suggestions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: var(--smartie-space-sm);
  margin: var(--smartie-space-lg) 0 0;
  padding: 0;
  list-style: none;
}

.suggestion {
  min-block-size: 2.125rem;
  padding: var(--smartie-space-xs) var(--smartie-space-md);
  border: none;
  border-radius: var(--smartie-radius-pill);
  background-color: var(--smartie-color-paper-tint);
  color: var(--smartie-color-ink);
  font-size: var(--smartie-size-sm);
  line-height: var(--smartie-leading-tight);
  text-align: center;
}

@media (min-width: 48rem) {
  .intro {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    gap: var(--smartie-space-2xl);
    text-align: start;
  }

  /* The designs hold the robot off the container edge rather than letting it run
   * into the side of the page. */
  .intro__figure {
    order: 1;
    margin-inline-end: var(--smartie-figure-inset);
  }

  .intro__copy {
    align-items: flex-start;
    flex: 1 1 auto;
    min-inline-size: 0;
    max-inline-size: 47rem;
  }

  .suggestions {
    justify-content: flex-start;
  }

  .suggestion {
    text-align: start;
  }
}

/* ---------------------------------------------------------------------------
 * Composer
 *
 * One bordered box with the send button inside it, in both the places it lives:
 * under the greeting while the chat is empty, and at the bottom of the panel
 * once it is not.
 * ------------------------------------------------------------------------ */

.composer {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: var(--smartie-space-sm);
  inline-size: 100%;
  padding-inline: var(--smartie-space-md);
  padding-block: var(--smartie-space-xs);
  border: var(--smartie-border-hairline) solid var(--smartie-color-brand);
  border-radius: var(--smartie-radius-field);
  background-color: var(--smartie-color-paper);

  /* While an answer is on its way the field is made read-only rather than disabled, so
   * the caret and anything already typed stay where they are. Read-only changes nothing
   * on screen by itself, so the box sinks, gives up its blue edge, and takes the send
   * button's colour with it for as long as it will not accept a message.
   *
   * The writing stays at full strength on the sunk ground. A greyed placeholder on a
   * grey box is the one thing here that would be harder to read than what it replaces.
   *
   * Declared above :focus-within so a field still holding the caret keeps its ring. */
  &[aria-busy="true"] {
    border-color: var(--smartie-color-rule);
    background-color: var(--smartie-color-paper-sunk);

    & .composer__field,
    & .composer__send {
      cursor: not-allowed;
    }

    & .composer__send {
      background-color: var(--smartie-color-ink-faint);
    }
  }

  /* The ring goes on the box rather than on the field inside it. A textarea matches
   * :focus-visible the whole time it is focused, and an outline drawn just inside the
   * border would read as a second border for as long as someone is typing. */
  &:focus-within {
    border-color: var(--smartie-color-focus);
    box-shadow: 0 0 0 var(--smartie-border-hairline) var(--smartie-color-focus);
  }
}

/* Holds the field and its label together, so the label can sit against the typing
 * area rather than against the box's padding, which changes with the breakpoint. */
.composer__field-wrap {
  position: relative;
  min-inline-size: 0;
}

.composer__field {
  display: block;
  inline-size: 100%;
  max-block-size: 8rem;
  /* The space above the text is the room the label rises into. */
  padding-block: var(--smartie-space-md) 0;
  padding-inline: 0;
  border: none;
  background-color: transparent;
  color: var(--smartie-color-ink);
  font: inherit;
  line-height: var(--smartie-leading-body);
  resize: none;

  &:focus-visible {
    outline: none;
  }

  /* The label carries the same words and never leaves, so the placeholder is
   * only here to tell an empty field from a filled one. */
  &::placeholder {
    color: transparent;
  }
}

/* Centred in the box, because while it reads as the placeholder that is where a
 * placeholder belongs. Lining it up with the first line of typing instead would
 * sit it low, since the room above that line is reserved for the label itself. */
.composer__label {
  position: absolute;
  inset-block-start: 50%;
  inset-inline-start: 0;
  translate: 0 -50%;
  color: var(--smartie-color-ink);
  pointer-events: none;
}

/* Up into the room the field's top padding left, and small enough to stay out of
 * the way of the typing. */
.composer__field:focus + .composer__label,
.composer__field:not(:placeholder-shown) + .composer__label {
  inset-block-start: 0;
  translate: 0 0;
  font-size: var(--smartie-size-xs);
  color: var(--smartie-color-brand);
}

.composer__send {
  position: relative;
  display: grid;
  place-items: center;
  inline-size: var(--smartie-send-size);
  block-size: var(--smartie-send-size);
  border-radius: var(--smartie-radius-pill);
  background-color: var(--smartie-color-brand);
  color: var(--smartie-color-on-brand);

  /* The design draws a 32px button. This is the 44px a finger needs, laid over
   * it and invisible. */
  &::before {
    content: "";
    position: absolute;
    inset-block-start: 50%;
    inset-inline-start: 50%;
    inline-size: var(--smartie-target-min);
    block-size: var(--smartie-target-min);
    translate: -50% -50%;
  }

  /* The design's own chevron, drawn at the size it was drawn at. */
  & svg {
    inline-size: 0.5rem;
    block-size: 0.8125rem;
    fill: currentColor;
  }

  &:disabled {
    cursor: not-allowed;
  }
}

/* Sits over the gap under the box, so appearing and disappearing never moves
 * anything. */
.composer__count {
  position: absolute;
  inset-block-start: calc(100% + var(--smartie-space-2xs));
  inset-inline-end: 0;
  font-size: var(--smartie-size-xs);
  color: var(--smartie-color-ink-faint);
}

@media (min-width: 48rem) {
  .composer {
    padding-block: var(--smartie-space-sm);
  }
}

/* ---------------------------------------------------------------------------
 * Dock — everything pinned under the transcript.
 * ------------------------------------------------------------------------ */

.dock {
  flex: none;
  padding-inline: var(--smartie-shell-gutter);
  padding-block-end: max(var(--smartie-space-md), env(safe-area-inset-bottom));
  background-color: var(--smartie-color-paper);
}

.dock__inner {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: var(--smartie-space-sm);
}

/* Empty while the composer is up in the greeting, and an empty flex item still
 * earns a gap. */
.dock__composer:empty {
  display: none;
}

.legal {
  text-align: center;
}

/* The page slots in rich text, so what lands here is a wrapper around paragraphs
 * rather than a paragraph. The paragraphs inside it are light DOM and are styled
 * by the site's own stylesheet; all this does is take the wrapper's margin off. */
::slotted(*) {
  margin: 0;
}

/* On a narrow panel the send button drops to its own row so the field keeps a
 * usable width. Keyed to the panel, not the viewport, because the widget can be
 * dropped into a sidebar on a wide screen. */
@container smartie-panel (width < 18rem) {
  .composer {
    grid-template-columns: 1fr;
  }

  .composer__send {
    inline-size: 100%;
  }
}
