/* ==========================================================================
   Chatbot (/chat)
   Loaded after css/site.css by the pages of this family ('styles' in render_page()).
   Conventions and tokens: css/README.md.

   One panel, a grid of three rows: header / conversation / composer. The conversation is the only part that
   scrolls. The panel is a container ("chat"): the rules without a query are the narrow panel (a phone), the
   @container block near the end is the roomy one (from 36em of panel, i.e. tablets and desktops). A browser
   without container queries keeps the narrow layout, which is complete.

   From the core (site.css): the buttons (.btn-sm / .btn with .btn-secondary-action / .btn-primary-action), the
   small pill (.sample-pill-btn: "Copy"), the field (.portal-input-field), the table of an answer (.key-table in
   .l-scroll, put there by js/chatbot.js), .l-cluster, .visually-hidden, the focus ring and the touch targets.
   Built by js/chatbot.js: every .message after the first, the chips, the status line.
   ========================================================================== */
@layer pages {
  /* ---- The panel ----
     As tall as the window less the sticky header (--header-offset) and the gaps of the shell above and under it,
     so the composer is on screen without scrolling the page; never so short that nothing can be read. */
  .chat-container {
    container: chat / inline-size;
    isolation: isolate;   /* the key popover stacks inside the panel, under the sticky header of the site */
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr);
    grid-auto-rows: auto;
    inline-size: 100%;
    block-size: calc(100vh - var(--header-offset) - 2 * var(--space-main-start));
    block-size: calc(100dvh - var(--header-offset) - 2 * var(--space-main-start));
    min-block-size: 24rem;
    border: var(--border-width) solid var(--border-neutral);
    border-radius: var(--radius-lg);
    background-color: var(--surface-card);
    box-shadow: var(--shadow-card);
  }

  /* ---- Header: the title and the buttons, the status (who is asking, requests left) on a line under them ----
     The first column never gets narrower than the longest word of the title: the buttons then go one under the
     other instead (a very narrow phone with a long label on the key button). */
  .chat-header {
    position: relative;   /* the key popover of a narrow panel is placed against the header */
    display: grid;
    grid-template-columns: minmax(min-content, 1fr) auto;
    grid-template-areas:
      "title actions"
      "status status";
    align-items: center;
    gap: var(--space-1) var(--space-2);
    padding: var(--space-2-5) var(--space-3);
    border-block-end: var(--border-width) solid var(--border-neutral);
    border-radius: calc(var(--radius-lg) - var(--border-width)) calc(var(--radius-lg) - var(--border-width)) 0 0;
    background-color: var(--surface-sunken);
  }

  .chat-title {
    grid-area: title;
    font-size: var(--fs-lg-plus);
    font-weight: 700;
    line-height: 1.3;
  }

  .chat-status {
    grid-area: status;
    min-inline-size: 0;
    font-size: var(--fs-sm);
    line-height: 1.4;
  }

  .chat-header-actions {
    --cluster-justify: flex-end;
    grid-area: actions;
  }

  .chat-mode-session {
    color: var(--text-neutral-subtle);
  }

  .chat-mode-apikey {
    font-weight: 600;
    color: var(--text-link);
  }

  /* the address of a signed-in customer: it wraps rather than being cut */
  .user-logged-badge {
    display: inline-flex;
    align-items: baseline;
    gap: var(--space-1-5);
    max-inline-size: 100%;
    padding: var(--space-1) var(--space-2-5);
    border: var(--border-width) solid var(--border);
    border-radius: var(--radius-xl);
    background-color: var(--accent-tint-1);
    font-weight: 600;
    overflow-wrap: anywhere;
    color: var(--text-link);
  }

  .btn-reset,
  .btn-popover-trigger {
    white-space: nowrap;
  }

  .icon-key {
    color: var(--text-link);
  }

  /* a key is saved */
  .btn-popover-trigger.active {
    background-color: var(--accent-tint-3);
    border-color: var(--border-accent);
    color: var(--text-link);
  }

  /* ---- The API key popover (signed-in customers never see it: the script hides .popover-container) ---- */
  .popover-menu {
    display: none;
    position: absolute;
    inset-block-start: calc(100% + var(--space-2));
    inset-inline-end: var(--space-3);
    z-index: var(--z-dropdown);
    inline-size: min(20rem, 100% - 2 * var(--space-3));
    padding: var(--space-4);
    border: var(--border-width) solid var(--border-neutral-strong);
    border-radius: var(--radius-md);
    background-color: var(--surface-card);
    box-shadow: var(--shadow-dropdown);
  }

  .popover-menu.open {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: var(--space-2-5);
  }

  .popover-title {
    display: flex;
    align-items: center;
    gap: var(--space-1-5);
    font-size: var(--fs-md-plus);
    font-weight: 700;
    line-height: 1.3;
    color: var(--text-neutral-strong);
  }

  .popover-text {
    font-size: var(--fs-md-plus);
    line-height: 1.4;
    color: var(--text-neutral-subtle);
  }

  .popover-text a {
    text-decoration: underline;
  }

  .popover-actions {
    --cluster-justify: flex-end;
  }

  /* ---- The conversation: the only scrolling area ---- */
  .chat-messages {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    align-content: start;
    gap: var(--space-3);
    padding: var(--space-4) var(--space-3);
    overflow-y: auto;
    overscroll-behavior: contain;
    scroll-behavior: smooth;
    background-color: var(--surface-sunken);
  }

  .chat-messages:focus-visible {
    outline-offset: -2px;   /* inside: the panel would cut it off */
  }

  /* A bubble: as wide as its text, never wider than a readable line. The transparent border is what forced
     colours draw around a bubble that only has a background. */
  .message {
    justify-self: start;
    min-inline-size: 0;
    max-inline-size: min(90%, 80ch);
    padding: var(--space-3) var(--space-3-5);
    border: var(--border-width) solid transparent;
    border-radius: var(--radius-md);
    font-size: var(--fs-xl);
    line-height: 1.6;
    overflow-wrap: anywhere;
  }

  .message.user {
    justify-self: end;
    border-bottom-right-radius: var(--space-0-5);
    background-color: var(--accent);
    white-space: pre-wrap;
    color: var(--text-on-accent);
  }

  .message.assistant {
    border-color: var(--border-neutral);
    border-bottom-left-radius: var(--space-0-5);
    background-color: var(--surface-card);
    box-shadow: var(--shadow-card);
    color: var(--text-neutral-strong);
  }

  .message.error {
    border-color: var(--danger-border-alt);
    border-bottom-left-radius: var(--space-0-5);
    background-color: var(--danger-bg-soft);
    color: var(--danger-text-alt);
  }

  .error-title {
    display: flex;
    align-items: baseline;
    gap: var(--space-1-5);
    font-weight: 700;
  }

  .error-body {
    margin-block-start: var(--space-1);
    font-size: var(--fs-lg);
    line-height: 1.45;
    color: var(--danger-text-deep);
  }

  .error-body a,
  .message-content a {
    text-decoration: underline;
  }

  /* ---- An answer: Markdown rendered by the server (includes/markdown.php), filtered by js/safe-html.js.
     No element of it carries a class, hence the element selectors under .message-content. ---- */
  .message-content > :first-child {
    margin-block-start: 0;
  }

  .message-content > :last-child {
    margin-block-end: 0;
  }

  /* An answer shown as plain text (no server-rendered HTML came with it): its line breaks are kept */
  .message-content.pre-line {
    white-space: pre-line;
  }

  .message-content p {
    margin-block-end: var(--space-3);
  }

  .message-content :is(h1, h2, h3, h4, h5, h6) {
    margin-block: var(--space-4) var(--space-2);
    padding-block-end: var(--space-1);
    border-block-end: var(--border-width) solid var(--border-neutral-faint);
    font-size: var(--fs-2xl);
    font-weight: 700;
    line-height: 1.35;
    color: var(--text-link);
  }

  .message-content strong {
    font-weight: 600;
    color: var(--text-strong);
  }

  .message-content :is(ul, ol) {
    margin-block: var(--space-2) var(--space-3-5);
    padding-inline-start: var(--space-6);
  }

  .message-content li {
    margin-block-end: var(--space-1-5);
    line-height: 1.55;
  }

  .message-content li > :is(ul, ol) {
    margin-block: var(--space-1-5) 0;
  }

  .message-content code {
    padding: var(--space-0-5) var(--space-1-5);
    border-radius: var(--radius-xs);
    background-color: var(--surface-neutral);
    font-family: var(--font-mono);
    font-size: max(0.75rem, 0.88em);
    font-weight: 600;
    color: var(--text-link);
  }

  /* a block of code wraps: nothing to scroll sideways, nothing cut */
  .message-content pre {
    margin-block: var(--space-3);
    padding: var(--space-3);
    border: var(--border-width) solid var(--border-neutral);
    border-radius: var(--radius-sm);
    background-color: var(--surface-sunken);
    white-space: pre-wrap;
  }

  .message-content pre code {
    padding: 0;
    background-color: transparent;
    font-weight: 400;
    color: inherit;
  }

  .message-content blockquote {
    margin-block: var(--space-3);
    padding: var(--space-2-5) var(--space-3-5);
    border-inline-start: var(--space-1) solid var(--border-accent);
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
    background-color: var(--accent-tint-2);
    color: var(--text-link);
  }

  .message-content hr {
    margin-block: var(--space-4);
    border: 0;
    border-block-start: var(--border-width) solid var(--border-neutral);
  }

  /* A table of an answer (js/chatbot.js puts it in this wrapper and gives it the table of the core): it keeps
     the width its words need and scrolls sideways inside the bubble; a shadow shows on the side where more of
     it is hidden (the two covers move with the content and hide the shadow at each end). */
  .message-table {
    margin-block: var(--space-3);
    border: var(--border-width) solid var(--border-neutral);
    border-radius: var(--radius-sm);
    background:
      linear-gradient(to right, var(--surface-card) 40%, transparent) left center / var(--space-8) 100% no-repeat local,
      linear-gradient(to left, var(--surface-card) 40%, transparent) right center / var(--space-8) 100% no-repeat local,
      linear-gradient(to right, var(--overlay-edge), transparent) left center / var(--space-3) 100% no-repeat scroll,
      linear-gradient(to left, var(--overlay-edge), transparent) right center / var(--space-3) 100% no-repeat scroll;
    overflow-wrap: break-word;   /* whole words in the cells: the bubble's "anywhere" would squeeze the columns */
  }

  .message-table:focus-visible {
    outline-offset: -2px;
  }

  .message-table .key-table {
    margin-block-start: 0;
  }

  .message-table :is(th, td) {
    padding: var(--space-2) var(--space-3);
    vertical-align: top;
  }

  .message-table tr:last-child > td {
    border-block-end: 0;
  }

  /* ---- Under an answer: the suggested questions, then "Copy" ---- */
  .suggestions-wrapper {
    display: grid;
    gap: var(--space-1-5);
    margin-block-start: var(--space-3-5);
    padding-block-start: var(--space-2-5);
    border-block-start: var(--border-width) dashed var(--border-neutral);
  }

  .suggestions-title {
    display: flex;
    align-items: center;
    gap: var(--space-1-5);
    font-size: var(--fs-xs);
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--text-neutral-subtle);
  }

  /* A chip: a question to ask with one click. A long one wraps inside its chip. */
  .btn-suggestion {
    position: relative;
    padding: var(--space-1-5) var(--space-3);
    border: var(--border-width) solid var(--border-accent-pale);
    border-radius: var(--radius-xl);
    background-color: var(--accent-tint-2);
    font-size: var(--fs-sm);
    font-weight: 500;
    line-height: 1.35;
    text-align: start;
    color: var(--text-link);
    transition: background-color var(--duration-fast) ease, border-color var(--duration-fast) ease, color var(--duration-fast) ease;
  }

  .message-actions {
    display: flex;
    justify-content: flex-end;
    margin-block-start: var(--space-3);
    padding-block-start: var(--space-2);
    border-block-start: var(--border-width) solid var(--border-neutral-faint);
  }

  .btn-copy-msg {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1-5);
    font-weight: 600;
  }

  .btn-copy-msg.copied {
    background-color: var(--done-bg);
    border-color: var(--done-border);
    color: var(--done-text);
  }

  /* ---- Waiting for the answer ---- */
  .typing-dots {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1-5);
    padding: var(--space-1) var(--space-0-5);
  }

  .typing-dots span {
    inline-size: var(--space-2);
    block-size: var(--space-2);
    border-radius: 50%;
    background-color: var(--accent);
    animation: typingBounce 1.4s infinite ease-in-out both;
  }

  .typing-dots span:nth-child(1) {
    animation-delay: -0.32s;
  }

  .typing-dots span:nth-child(2) {
    animation-delay: -0.16s;
  }

  @keyframes typingBounce {
    0%, 80%, 100% {
      transform: scale(0);
      opacity: 0.3;
    }
    40% {
      transform: scale(1);
      opacity: 1;
    }
  }

  /* ---- The composer: the field (the field of the core) and "Send" ----
     The field starts two lines of text tall and grows with what is typed (js/chatbot.js sets its height) up to
     its maximum, then scrolls. "Send" is as tall as the empty field and stays at the bottom.
     On a narrow panel the placeholder is longer than two lines: the small padding keeps its third line out of
     sight, and an empty field has nothing to scroll. */
  .chat-input-area {
    --chat-field-pad: var(--space-1);
    --chat-field-min: calc(3rem + 2 * var(--chat-field-pad) + 2 * var(--border-width));
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: end;
    gap: var(--space-2);
    padding: var(--space-2-5) var(--space-3);
    border-block-start: var(--border-width) solid var(--border-neutral);
    border-radius: 0 0 calc(var(--radius-lg) - var(--border-width)) calc(var(--radius-lg) - var(--border-width));
    background-color: var(--surface-card);
  }

  .chat-input {
    min-block-size: var(--chat-field-min);
    max-block-size: 12.5rem;
    padding-block: var(--chat-field-pad);
    overflow-y: auto;
    font-size: var(--fs-xl);
    font-weight: 400;
    resize: none;
    /* when the browser brings the focused field into view (on-screen keyboard), the edge of the panel comes too */
    scroll-margin-block-end: calc(var(--space-4) + var(--safe-bottom));
  }

  .chat-input:placeholder-shown {
    overflow-y: hidden;
  }

  .chat-send {
    min-block-size: var(--chat-field-min);
    padding-inline: var(--space-4);
    font-size: var(--fs-xl);
  }

  .kbd-enter {
    display: inline-grid;
    place-items: center;
    inline-size: var(--space-5);
    block-size: var(--space-5);
    border: var(--border-width) solid var(--border-on-accent);
    border-radius: var(--radius-xs);
    font-family: inherit;
    font-size: var(--fs-sm);
    line-height: 1;
  }

  /* Without JavaScript: the note between the conversation and the composer */
  .chat-noscript {
    margin: var(--space-3);
  }

  /* ---- A roomy panel: one line for the header, more air ---- */
  @container chat (min-width: 36em) {
    .chat-header {
      grid-template-columns: max-content minmax(0, 1fr) auto;
      grid-template-areas: "title status actions";
      column-gap: var(--space-3);
      padding: var(--space-3-5) var(--space-6);
    }

    .chat-status {
      justify-self: end;
      text-align: end;
    }

    /* the popover hangs from its button */
    .popover-container {
      position: relative;
    }

    .popover-menu {
      inset-inline-end: 0;
      inline-size: 20rem;
    }

    .chat-messages {
      gap: var(--space-4);
      padding: var(--space-6);
    }

    .message {
      padding: var(--space-4) var(--space-5);
    }

    .chat-input-area {
      --chat-field-pad: var(--space-3);
      gap: var(--space-3);
      padding: var(--space-3-5) var(--space-6);
    }

    .chat-send {
      padding-inline: var(--space-6);
    }
  }

  /* ---- Pointer feedback ---- */
  @media (hover: hover) {
    .btn-suggestion:hover {
      background-color: var(--accent);
      border-color: var(--border-accent);
      color: var(--text-on-accent);
    }
  }

  /* ---- Touch: a chip is at least 44px to touch, without being drawn any bigger (as the core does) ---- */
  @media (pointer: coarse) {
    .btn-suggestion::before {
      content: "";
      position: absolute;
      inset-block: min(0rem, (100% - var(--target-min)) / 2);
      inset-inline: min(0rem, (100% - var(--target-min)) / 2);
    }
  }

  /* ---- Less motion: three dots that stand still (the animation would end on its first, invisible, frame) ---- */
  @media (prefers-reduced-motion: reduce) {
    .typing-dots span {
      animation: none;
    }
  }

  /* ---- Forced colours: what is drawn with a background only ---- */
  @media (forced-colors: active) {
    .typing-dots span {
      background-color: CanvasText;
    }

    .btn-popover-trigger.active {
      border-color: Highlight;
    }
  }

  /* ---- Paper: the whole conversation, not the part that was on screen ---- */
  @media print {
    .chat-container {
      display: block;
      block-size: auto;
      min-block-size: 0;
      border: 0;
      box-shadow: none;
    }

    .chat-header-actions,
    .suggestions-wrapper,
    .message-actions {
      display: none;
    }

    .chat-messages {
      overflow: visible;
    }

    .message {
      max-inline-size: none;
      border-color: currentColor;
      background: none;
      box-shadow: none;
      break-inside: avoid;
    }
  }
}
