/* ============================================================
   CHAT KEYBOARD MODE — phones only. While the chat input has focus and
   the keyboard is up, the bot slides up so the eyes sit at the top of
   the area the keyboard leaves visible, and the input floats over it,
   just above the keyboard:

     ┌──────────────────────┐  ← top of the VISIBLE area (above keyboard)
   ┌─┼──────────────────────┼─┐
   │ │  frame top + eyes    │ │  the frame and chat box at their normal
   │ │  ┌────────────────┐  │ │  size, nothing clipped or resized —
   │ │  │  chat          │  │ │  only moved up together
   │ │  │                │  │ │
   │ │  │░░░░░░░░░░░░░░░░│  │ │  ← cream strip: the chat box's own colour,
   │ │  input (floats on top) │    laid over the part the input covers, so
   │ │  ...frame carries on   │    the chat AREA ends above the input while
   └─┼── under the keyboard ──┼─┘  the box still fills the frame's opening
     └──────────────────────┘  ← keyboard starts here

   Why this exists: iOS never shrinks the page for the keyboard — it pans
   the whole page up until the focused input clears it, which is what
   dragged the frame off screen. So the input (and the things we want
   visible) are put inside the area that will be left above the keyboard,
   measured by chat-keyboard.js from the visualViewport and handed over
   as --kb-top / --kb-height.

   Why nothing is resized (Nov 2, 2026): the first version of this mode
   pulled the chat box out of the frame and stretched it edge to edge. A
   different width re-wraps every message, so the conversation changes
   height and every scroll position points somewhere else the moment the
   keyboard opens or closes — the source of the phone scroll bugs. Here
   the box keeps its exact size, so the conversation is laid out the same
   with the keyboard up or down. The only extra is a little scroll room
   at the end (the height of the cream strip), so the last line can still
   come up above the input.

   Scoped twice: phone widths AND body.chat-keyboard (added only on a
   touch device, only while #user-input has focus). Desktop never sees
   any of this.
   ============================================================ */

:root {
  /* Defaults until chat-keyboard.js measures the visible area. */
  --kb-top: 0px;
  --kb-height: var(--viewport-height, 100vh);
}

@media (max-width: 599px) {
  body.chat-keyboard {
    --kb-margin: 6px;    /* breathing room under the address bar */
    --kb-input: 60px;    /* the input row */
    --kb-gap: 14px;      /* cream between the last line of chat and the input */
    /* Where the chat AREA ends, measured from the frame's top edge:
       the gap above the input. */
    --kb-chat-end: calc(var(--kb-height) - var(--kb-input) - var(--kb-gap) - var(--kb-margin));
    /* Where the chat BOX ends (the frame's opening), same measure. */
    --kb-box-end: calc(var(--frame-size) * (var(--chat-top-pct) + var(--chat-height-pct)));
    /* How much of the box sits below the chat area's end — the cream
       strip's height, and the extra scroll room. 0 when it all fits. */
    --kb-cover: max(0px, calc(var(--kb-box-end) - var(--kb-chat-end)));
  }

  /* The frame: slid to the top of the visible area, normal size,
     nothing clipped. Below the input it carries on under the keyboard. */
  body.chat-keyboard #chatbot-overlay {
    top: calc(var(--kb-top) + var(--kb-margin));
    transform: translateX(-50%);
  }

  /* The chat box: moved up with the frame. Same size, same place in the
     opening — nothing else changes. */
  body.chat-keyboard #chat-box {
    top: calc(var(--kb-top) + var(--kb-margin) + var(--frame-size) * var(--chat-top-pct));
  }

  /* The cream strip. It lives in the frame, which paints above the chat
     box, so it hides the chat behind the input; it sits under the frame
     art (z-index 5) so the frame's inner edge still overlaps it the way
     it overlaps the box. Same colour as #chat-box in chatbot.css — keep
     them in step. */
  body.chat-keyboard .frame-wrapper::after {
    content: '';
    position: absolute;
    z-index: 4;
    left: calc(var(--frame-size) * var(--chat-left-pct));
    width: calc(var(--frame-size) * var(--chat-width-pct));
    top: calc(var(--kb-box-end) - var(--kb-cover));
    height: var(--kb-cover);
    background: #FFFFF0;
    pointer-events: none;
  }

  /* The extra scroll room, so the last line can come up above the
     strip. Set on the spacer's margin — chatbot.js owns its height. */
  body.chat-keyboard #chat-spacer { margin-bottom: var(--kb-cover); }

  /* The input: pinned just above the keyboard, floating over the frame. */
  body.chat-keyboard .external-chat-input {
    bottom: auto;
    top: calc(var(--kb-top) + var(--kb-height) - var(--kb-input));
    width: 90vw;
    max-width: none;
  }
  /* Behind it, a dark band instead of the usual soft halo: it now floats
     over the busy frame, so it needs real contrast. Full width, a short
     fade at the top edge so it doesn't read as a hard bar, and it runs
     on down behind iOS's AutoFill bar to the keyboard. The two numbers
     to taste: the 0.85 (how dark) and the blur (how much the frame
     behind is softened). */
  body.chat-keyboard .external-chat-input::before {
    top: -24px;
    bottom: -160px;
    left: -5vw;              /* the input is 90vw wide and centred */
    right: -5vw;
    border-radius: 0;
    filter: none;
    background: linear-gradient(to bottom,
      rgba(10, 8, 12, 0) 0,
      rgba(10, 8, 12, 0.85) 24px,
      rgba(10, 8, 12, 0.85) 100%);
    -webkit-backdrop-filter: blur(3px);
    backdrop-filter: blur(3px);
  }

  /* Nothing to steer while typing, and they'd land on the input. */
  body.chat-keyboard .nav-arrow {
    opacity: 0;
    pointer-events: none;
  }

  @media (max-width: 399px) {
    body.chat-keyboard { --kb-input: 50px; }   /* the smaller send button */
  }

  /* Once the mode is on, follow the keyboard's real height smoothly
     (the first estimate → the measured value, or a rotation). The switch
     into the mode itself is instant — chat-keyboard.js adds -live a frame
     later — so nothing tweens between the two layouts. */
  body.chat-keyboard-live #chatbot-overlay     { transition: top 0.18s ease, opacity 0.3s ease; }
  body.chat-keyboard-live #chat-box            { transition: top 0.18s ease, opacity 0.2s ease; }
  body.chat-keyboard-live .external-chat-input { transition: top 0.18s ease; }
}