/* ==========================================================================
   Chat page, desktop layout (body.chat-desk; config
   CHAT_DESKTOP_LAYOUT_ENABLED, ?chat_desk_preview=1 / =0;
   docs/agents/conversations.md, "Desktop layout").

   The owner's design of 2026-11-21, chosen step by step from drawings:

   - a frosted bar across the top of the window: the chat list's menu button,
     then the places to go (Chat, Story Generator, Updates) starting right
     beside it, with a line under the current one; the support and account
     links at the right, with no divider between them;
   - the title on the page background, close under the bar, with the
     character's picture at the card's left edge and New Chat at its right
     edge on the same line (no tagline);
   - a card sized to the conversation (920px, or the window less 20px each
     side; the owner found a wider card left a large unused area, and a
     short reply too far from the message it answers) that holds only the
     conversation: no box around the messages, the messages no wider than
     about today's, and the typing area, the voice button and Send on one
     row; the bar's row is as wide as the card, and grows only when its
     links need more (static/chat_desk.js sets --desk-row-w);
   - the chat list docked at the left under the bar while it is open (it
     closes when the pointer leaves it, as it always has), the page
     re-centred in what is left; below 901px it slides over the page instead.

   Nothing here applies at or below 600px (static/css/chat_phone.css owns
   the phone layout) or without body.chat-desk, apart from the two base rules
   below that keep the switch's own elements out of the phone layout; and the
   stylesheet is only linked while the switch is on. The page's <h1> stays real, visible
   text (search relies on it); the tagline stays in the page but is hidden, as the phone
   layout hides it.

   Colours: this file is a source of scripts/build_dark_css.py, so every
   colour below gets its dark counterpart in static/css/dark.css; the ones
   that need choosing by hand (the picture's ring and gap) are in
   static/css/dark_extra.css. Media queries are kept flat (never nested).
   ========================================================================== */

/* Rendered only while the switch is on; shown only above 600px. (The bar's
   own rule is in desk_bar.css.) */
.chat-desk-only,
.chat-desk-close {
  display: none;
}

/* Below 601px the card is not a box: the phone layout sees its children as
   the container's own, exactly as without it. */
.chat-desk-card {
  display: contents;
}

@media (min-width: 601px) {
  /* ---- The page ---- */

  /* --desk-bar-h, --desk-dock, --desk-card-w and --desk-row-w are set on
     body.desk-bar (desk_bar.css); the card below is --desk-card-w wide. */
  body.chat-page.chat-desk {
    /* 12px under the Disclaimer line: the whole page fits a 900px-tall window. */
    padding: calc(var(--desk-bar-h) + 18px) 0 12px var(--desk-dock);
    transition: padding-left 0.3s ease;
  }

  /* ---- The bar ----
     The bar itself is static/css/desk_bar.css (shared with the other pages
     that have it), including the room at its left end for the chat list's
     menu button, which this page places there (below). */

  /* ---- The chat list's menu button, over the bar's left end ----
     Fixed like the bar, and placed by the same arithmetic as the bar's row
     (percentages of the window, so a scrollbar shifts both alike). Signed-out
     visitors get one too (#guest-menu-toggle, .chat-menu-only elsewhere). */
  body.chat-page.chat-desk .sidebar-toggle,
  body.chat-page.chat-desk .sidebar-toggle.chat-menu-only {
    top: 10px;
    left: calc(var(--desk-dock) + max(20px, (100% - var(--desk-dock) - var(--desk-row-w)) / 2));
    display: flex !important;
    width: 40px;
    height: 40px;
    box-shadow: none;
    transition: left 0.3s ease, background-color 0.3s ease, color 0.3s ease;
  }

  /* ---- Title line: picture, title, New Chat ---- */

  body.chat-page.chat-desk #main-chat-container {
    position: relative;
    width: min(var(--desk-card-w), calc(100% - 40px));
    max-width: none;
    margin-right: auto;
    margin-left: auto;
    padding: 0;
    border-radius: 0;
    background: transparent;
    box-shadow: none;
  }

  /* The links rows of the old header: the bar has them now. */
  body.chat-page.chat-desk #main-chat-container .patreon-link,
  body.chat-page.chat-desk #main-chat-container .login-section {
    display: none;
  }

  /* The title, clear of the picture (left) and New Chat (right), with 31px
     under it to the card (about 20px under the picture's ring). Its size
     and line differ by width: the 901px block and the block for narrower
     windows, below. One line always: a second one would push the card down
     and make the page scroll (at 601px, and at 901px with the chat list
     docked, it has 309px; a wide system font, measured Verdana-width,
     needs 335px at 41px, the wide layout's first size; it is 38px now). */
  body.chat-page.chat-desk #main-chat-container .chat-title {
    margin: 0 0 31px;
    padding: 0 126px;
    white-space: nowrap;
  }

  /* No tagline (owner, 2026-11-22: the row of picture, title, tagline,
     underline and New Chat felt too crowded; nobody saw the tagline before a
     chat anyway, since the onboarding covers the page). It is hidden the way
     the phone layout hides it, and stays in the page. */
  body.chat-page.chat-desk #main-chat-container .chat-tagline {
    display: none;
  }

  /* The character's picture: Guided's round portrait with a lilac ring, the
     whole picture (not the phone header's face crop), alone at the card's
     left edge. Filled in by static/chat_menu.js; empty while hidden. */
  body.chat-page.chat-desk #main-chat-container #chat-portrait {
    position: absolute;
    top: -9px;
    left: 5px;
    z-index: 2;
    display: flex !important;
    align-items: center;
    height: 102px;
  }

  body.chat-page.chat-desk #chat-portrait > img,
  body.chat-page.chat-desk #chat-portrait > .portrait-crop,
  body.chat-page.chat-desk #chat-portrait .chat-portrait-initial {
    position: relative;
    box-sizing: border-box;
    flex: none;
    width: 102px;
    height: 102px;
    /* The gap between picture and ring is a border in the card's colour,
       which the dark scheme remaps (a white box-shadow it would not). */
    border: 3px solid #ffffff;
    border-radius: 50%;
    object-fit: cover;
    box-shadow: 0 0 0 3px #e9daff;
    cursor: pointer;
  }

  body.chat-page.chat-desk #chat-portrait > .portrait-crop {
    display: block;
    overflow: hidden;
  }

  /* chat_menu.js places a face crop with inline percentages: here the whole
     picture fills the circle. */
  body.chat-page.chat-desk #chat-portrait > .portrait-crop > img {
    position: static !important;
    display: block;
    width: 100% !important;
    max-width: none;
    height: 100% !important;
    margin: 0;
    border: 0;
    border-radius: 0;
    object-fit: cover;
    transform: none !important;
    translate: none !important;
  }

  body.chat-page.chat-desk #chat-portrait .chat-portrait-initial {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: #e9ddfb;
    color: #6b21a8;
    font-size: 36px;
    font-weight: 700;
  }

  /* Two characters in one chat (old AI Theater and three-way chats). */
  body.chat-page.chat-desk #chat-portrait > * + * {
    margin-left: -22px;
  }

  body.chat-page.chat-desk #chat-portrait > [role="button"]:focus-visible {
    outline: 2px solid #7e22ce;
    outline-offset: 5px;
  }

  /* New Chat at the card's right edge, its centre on the picture's centre
     line (owner, 2026-11-22: level with the title alone, it sat 18px above
     the picture's centre and the row looked uneven). The picture is 102px
     from 9px above the title's top, so its centre is 42px down; the button
     is 48px tall, so it starts 18px down. Above 900px the title is on that
     line too (the 901px block below), so the button stays there when the
     picture is hidden. From 601px to 900px (below) the smaller picture and
     the title are centred 26px down, and the button starts 2px down. */
  body.chat-page.chat-desk #main-chat-container #new-chat-button {
    position: absolute;
    top: 18px;
    right: 0;
    margin: 0;
  }

  /* ---- The card: only the conversation ---- */

  body.chat-page.chat-desk .chat-desk-card {
    display: block;
    /* None at the top: the message area reaches the card's top edge and
       keeps its own space there (see the fade below). */
    padding: 0 30px 30px;
    border-radius: 18px;
    background: #ffffff;
    box-shadow: 0 4px 20px rgba(138, 60, 200, 0.1);
  }

  /* No box around the messages (the card is the box), and as tall as the
     window allows: the page fits the window without scrolling down to a
     640px-tall one, and a taller one shows more of the conversation. 370px is
     everything else on the page (bar, title line, the card's bottom padding,
     the typing row, the Disclaimer line); the narrow-window block below has
     its own (its title row is shorter). */
  body.chat-page.chat-desk .chat-desk-card #chat-box {
    height: clamp(270px, calc(100vh - 370px), 2400px);
    height: clamp(270px, calc(100dvh - 370px), 2400px);
    /* None of the box's own; the rules below add the top and bottom (the
       fades) and the right (the scrollbar). */
    padding: 0;
    border: 0;
    border-radius: 0;
    background: transparent;
    box-shadow: none;
  }

  /* Without a box, a message scrolled part-way out was sliced through a line
     of text at an invisible edge (owner, 2026-11-22). The message area fades
     at its top and bottom instead, and it reaches the card's top edge, so a
     message scrolling up fades out at the card's edge and no band of the
     card stays blank above it (owner, 2026-11-22: the blank 30px of card
     padding plus the space under it was "too much"). Both fades lie inside
     spacing that is empty at rest: the first message sits 30px below the
     card's top, the same as the card's side and bottom padding (its 8px and
     the 22px added here), and whatever comes last ends at least 16px above
     the bottom (the 16px added here: a reply has its 12px margin as well,
     an error notice or an open edit's buttons have none), so nothing is
     faded when the chat is scrolled to either end. A reply held at the top
     while it streams is held at the same 30px (--reply-hold-margin, read by
     script.js replyHoldScrollTop; 12px in the other layouts), clear of the
     fade. One known exception, only softened, not hidden: the edit pencil
     under a last message of the reader's that got no reply. The mask is
     fixed to the area's edges; the messages scroll under it. */
  body.chat-page.chat-desk .chat-desk-card #chat-box {
    --reply-hold-margin: 30px;
    padding-top: 22px;
    padding-bottom: 16px;
    -webkit-mask-image: linear-gradient(to bottom, transparent 0, #000 24px, #000 calc(100% - 16px), transparent 100%);
    mask-image: linear-gradient(to bottom, transparent 0, #000 24px, #000 calc(100% - 16px), transparent 100%);
  }

  /* The scrollbar sits in the card's right margin, not on the messages
     (owner, 2026-11-22). With no box of its own the message area ended at
     the messages' right edge, so an overlay scrollbar (a Mac) was drawn over
     the right end of the reader's messages and the edit pencil, and an
     always-visible one (Windows, 17px) stood inside the card and pushed
     those messages 17px in from Send's edge. The area now reaches 22px into
     the card's 30px padding and gives that back as padding less the
     scrollbar's width, so the messages' column is as wide as before and the
     scrollbar ends 8px from the card's edge.
     The scrollbar is the browser's own, thin and lilac like the chat
     list's, through the standard properties only. NOT ::-webkit-scrollbar:
     that turns a Mac's overlay scrollbar (shown only while scrolling) into
     one that is always there, which the owner did not want (2026-11-22;
     measured in Chromium 151 and WebKit 26.5: 8px with ::-webkit-scrollbar,
     0 with these properties). So on a Mac it appears while scrolling, as
     everywhere else on the system, and where scrollbars are always visible
     (Windows) it is a thin lilac one.
     Its width differs by system (0 for an overlay one), so chat_desk.js
     measures it into --desk-sbw. It is always laid out (overflow-y:
     scroll), so the column does not change width when a chat grows long
     enough to scroll; the clear track shows nothing until there is
     something to scroll.
     Never sideways: a new message of the reader's slides in from 20px to the
     right (slideLeft), which can be further than the padding left here, and
     for that moment the area would be scrollable sideways and lose a
     scrollbar's height (chat_phone.css hides it for the same reason). */
  body.chat-page.chat-desk .chat-desk-card #chat-box {
    margin-right: -22px;
    padding-right: calc(22px - var(--desk-sbw, 0px));
    overflow-x: hidden;
    overflow-y: scroll;
    scrollbar-width: thin;
    scrollbar-color: rgba(138, 60, 200, 0.3) transparent;
  }

  /* Signed in, script.js keeps a 1px marker above the first message for
     loading older ones (#scrollback-top-sentinel), and in a chat started on
     this page the marker for newer ones sits right after it, above the
     first message too (the messages were appended after both). Each would
     push the first message down a pixel, and behind two markers the bubble
     kept its full margin (12px, or 24px for the reader's): 36px below the
     card's top instead of 30. The markers keep their 1px boxes (script.js
     observes them) and give the pixel back with a negative margin, and the
     first bubble keeps the 8px it has after one marker. */
  body.chat-page.chat-desk .chat-desk-card #chat-box > #scrollback-top-sentinel,
  body.chat-page.chat-desk .chat-desk-card #chat-box > #scrollback-top-sentinel + #scrollback-bottom-sentinel {
    margin-bottom: -1px;
  }

  body.chat-page.chat-desk .chat-desk-card #chat-box > #scrollback-top-sentinel + #scrollback-bottom-sentinel + .ai-message,
  body.chat-page.chat-desk .chat-desk-card #chat-box > #scrollback-top-sentinel + #scrollback-bottom-sentinel + .user-message {
    margin-top: 0.5rem;
  }

  /* Lines stay about as long as today's however wide the card is. */
  body.chat-page.chat-desk .chat-desk-card #chat-box .ai-message,
  body.chat-page.chat-desk .chat-desk-card #chat-box .user-message {
    max-width: min(640px, 80%);
  }

  /* One row: the typing area to the card's edge, then voice, then Send.
     script.js shows and hides the section with an inline display, so the row
     is forced and the hidden state is matched on that inline style. */
  body.chat-page.chat-desk .chat-desk-card #message-input-section {
    display: flex !important;
    align-items: center;
    gap: 12px;
  }

  body.chat-page.chat-desk .chat-desk-card #message-input-section[style*="display: none"],
  body.chat-page.chat-desk .chat-desk-card #message-input-section[style*="display:none"] {
    display: none !important;
  }

  body.chat-page.chat-desk .chat-desk-card #message-input {
    flex: 1 1 auto;
    width: auto;
    min-width: 0;
    max-width: none;
  }

  body.chat-page.chat-desk .chat-desk-card .input-controls {
    flex: none;
    flex-direction: row-reverse;
    gap: 10px;
    margin: 0;
  }

  /* Send's own right margin (an id rule) would hold it 10px off the edge. */
  body.chat-page.chat-desk .chat-desk-card #message-input-section .input-controls > * {
    margin: 0;
  }

  /* ---- The chat list panel ---- */

  body.chat-page.chat-desk .chat-sidebar,
  body.chat-page.chat-desk .chat-sidebar.chat-menu-only {
    display: flex !important;
  }

  body.chat-page.chat-desk .chat-sidebar .sidebar-content {
    padding-top: 20px;
  }

  body.chat-page.chat-desk .chat-sidebar .sidebar-top {
    padding-top: 0;
  }

  body.chat-page.chat-desk .chat-sidebar .sidebar-header,
  body.chat-page.chat-desk .chat-sidebar .sidebar-header.chat-desk-only {
    position: relative;
    display: flex;
  }

  /* The account links are in the bar. */
  body.chat-page.chat-desk .chat-sidebar .sidebar-account {
    display: none;
  }

  body.chat-page.chat-desk .chat-sidebar .chat-desk-close {
    position: absolute;
    top: -7px;
    right: -8px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    padding: 0;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: #6b7280;
    cursor: pointer;
  }

  body.chat-page.chat-desk .chat-sidebar .chat-desk-close:hover {
    background: #efe7fa;
    color: #4b3f63;
  }

  /* "Archived ›" sits at the header's right end, where the close button is
     now: it moves to the left end (the heading is centred between them). */
  body.chat-page.chat-desk .chat-sidebar .sidebar-header .history-archived-link {
    right: auto;
    left: 0;
  }

  /* Signed out: the phone menu's log-in card, where the chats would be. */
  body.chat-page.chat-desk .chat-menu-guest {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 16px;
    border: 1px solid #e9e0f7;
    border-radius: 14px;
    background: #ffffff;
    text-align: left;
  }

  body.chat-page.chat-desk .chat-menu-guest b {
    color: #2b1d42;
    font-size: 16px;
  }

  body.chat-page.chat-desk .chat-menu-guest span {
    color: #6b7280;
    font-size: 14px;
    line-height: 1.45;
  }

  body.chat-page.chat-desk .chat-menu-guest-btn {
    align-self: flex-start;
    margin-top: 6px;
    padding: 9px 16px;
    border-radius: 999px;
    background: linear-gradient(90deg, #8a3cc8, #732aa9);
    color: #ffffff;
    font-size: 14.5px;
    font-weight: 600;
    text-decoration: none;
  }

  /* The way back for a hidden picture: at the bottom of the panel, only
     while it is hidden (the same place as on the phone). */
  body.chat-page.chat-desk .chat-menu-show-picture,
  body.chat-page.chat-desk .chat-menu-show-picture.chat-menu-only {
    display: none !important;
  }

  body.chat-page.chat-desk.chat-portrait-off .chat-menu-show-picture,
  body.chat-page.chat-desk.chat-portrait-off .chat-menu-show-picture.chat-menu-only {
    display: block !important;
    flex: none;
    width: 100%;
    padding: 14px 0 16px;
    border: 0;
    border-top: 1px solid #ece4f8;
    background: transparent;
    color: #7e22ce;
    font-family: inherit;
    font-size: 15px;
    font-weight: 600;
    text-align: center;
    cursor: pointer;
  }

  /* ---- Hiding the picture: the option under it, and the message after ----
     (static/chat_menu.js; the phone's own look, which the owner approved.) */
  body.chat-page.chat-desk .chat-portrait-bubble {
    position: fixed;
    z-index: 95;
    transform: translateX(-50%);
  }

  body.chat-page.chat-desk .chat-portrait-bubble::before {
    content: "";
    position: absolute;
    top: -6px;
    left: 50%;
    margin-left: -6px;
    border: 6px solid transparent;
    border-top: 0;
    border-bottom-color: #2b1d42;
  }

  body.chat-page.chat-desk .chat-portrait-bubble button {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    min-height: 40px;
    padding: 0 14px;
    border: 0;
    border-radius: 10px;
    background: #2b1d42;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
    color: #ffffff;
    font-family: inherit;
    font-size: 14px;
    font-weight: 600;
    white-space: nowrap;
    cursor: pointer;
  }

  body.chat-page.chat-desk .chat-portrait-toast {
    position: fixed;
    left: calc(var(--desk-dock) + (100% - var(--desk-dock)) / 2);
    z-index: 95;
    display: flex;
    align-items: center;
    gap: 12px;
    box-sizing: border-box;
    width: max-content;
    max-width: calc(100% - var(--desk-dock) - 40px);
    padding: 10px 10px 10px 16px;
    border-radius: 12px;
    background: #2b1d42;
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.25);
    color: #ffffff;
    font-size: 14px;
    line-height: 1.35;
    transform: translateX(-50%);
  }

  body.chat-page.chat-desk .chat-portrait-toast span {
    flex: 1 1 auto;
  }

  body.chat-page.chat-desk .chat-portrait-toast button {
    flex: none;
    min-height: 36px;
    padding: 0 12px;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: #d8b4fe;
    font-family: inherit;
    font-size: 14px;
    font-weight: 700;
    cursor: pointer;
  }
}

/* ---- Wide enough to dock the chat list (901px and up) ---- */
@media (min-width: 901px) {
  body.chat-page.chat-desk.sidebar-open {
    --desk-dock: 300px;
  }

  /* The title stands alone (no tagline), larger so it does not look
     stranded beside the 102px picture (38px: the owner first chose 41px,
     halfway between the 38px and 44px drawings, 2026-11-22, then asked for
     slightly smaller, 2026-11-24), and on the picture's centre
     line: its line box is 84px from the title's top, and the picture's
     centre is 42px down. With the 31px under it the card is where it was
     with the tagline (84 + 31 is the old title, tagline and their margins:
     51 + 10 + 24 + 30), since height was not his reason. */
  body.chat-page.chat-desk #main-chat-container .chat-title {
    font-size: 38px;
    line-height: 84px;
  }

  /* Under the bar, with no shadow over the page beside it. */
  body.chat-page.chat-desk .chat-sidebar {
    top: var(--desk-bar-h);
    z-index: 80;
    height: calc(100vh - var(--desk-bar-h));
    height: calc(100dvh - var(--desk-bar-h));
    box-shadow: none;
  }
}

/* ---- Too narrow to dock (601px to 900px): the list slides over the page,
   the long wordings use the phone's short forms, and the title line is
   roomier ---- */
@media (min-width: 601px) and (max-width: 900px) {
  /* The title line, roomier (owner, 2026-11-22: the 102px picture looked
     crowded here): a smaller picture beside the title, at its 32px. */
  body.chat-page.chat-desk #main-chat-container #chat-portrait {
    top: -8px;
    left: 4px;
    height: 68px;
  }

  body.chat-page.chat-desk #chat-portrait > img,
  body.chat-page.chat-desk #chat-portrait > .portrait-crop,
  body.chat-page.chat-desk #chat-portrait .chat-portrait-initial {
    width: 68px;
    height: 68px;
    border-width: 2px;
    box-shadow: 0 0 0 2px #e9daff;
  }

  body.chat-page.chat-desk #chat-portrait .chat-portrait-initial {
    font-size: 26px;
  }

  body.chat-page.chat-desk #chat-portrait > * + * {
    margin-left: -14px;
  }

  /* On the 68px picture's centre line (26px down, where the title's line
     is centred too): the 48px button starts 2px down. */
  body.chat-page.chat-desk #main-chat-container #new-chat-button {
    top: 2px;
  }

  /* 33px less than the wide layout's 370px: the title's 51.2px line here
     against its 84px there (the smaller picture needs no more). The owner
     had the tagline removed here too (2026-11-22), which gave the
     conversation its line and margins: 43px, and 67px below 690px, where it
     wrapped. */
  body.chat-page.chat-desk .chat-desk-card #chat-box {
    height: clamp(270px, calc(100vh - 337px), 2400px);
    height: clamp(270px, calc(100dvh - 337px), 2400px);
  }

  body.chat-page.chat-desk .chat-sidebar {
    box-shadow: 4px 0 20px rgba(0, 0, 0, 0.15);
  }

  body.chat-page.chat-desk.sidebar-open::before {
    content: "";
    position: fixed;
    inset: 0;
    z-index: 99;
    background: rgba(0, 0, 0, 0.4);
  }

  /* The panel covers the menu button's place: its own close button closes it. */
  body.chat-page.chat-desk.sidebar-open .sidebar-toggle {
    visibility: hidden;
  }

  body.chat-page.chat-desk .chat-sidebar .sidebar-content {
    padding-top: 24px;
  }
}

/* ---- "Install App" for members on tablet-width windows (601px to 940px) ----
   Up to 940px the old page shows the account links at the top of the chat
   list, the only place the chat page offers Install App at those widths. The
   bar has Settings and Logout; the list keeps just Install App. */
@media (min-width: 601px) and (max-width: 940px) {
  body.chat-page.chat-desk .chat-sidebar .sidebar-account {
    display: flex;
    margin-top: 0;
  }

  body.chat-page.chat-desk .chat-sidebar .sidebar-account > :not([data-pwa-install-link]) {
    display: none !important;
  }
}
