/* The docked Quarterback: a chat in the corner of every other screen.
 *
 * The layout constraint that decides everything here is that the screen behind
 * it has to stay usable. So: no backdrop, no full-bleed sheet, a width that
 * leaves the page's own column readable on a desktop, and a height on a phone
 * that stops well short of the top of the screen. If this ever covers the thing
 * being asked about, the feature has eaten itself.
 */

.chat-dock {
  position: fixed;
  right: max(1rem, env(safe-area-inset-right));
  bottom: max(1rem, env(safe-area-inset-bottom));
  /* Above the dock (40) and the mobile home button (45), below any dialog. */
  z-index: 46;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.55rem;
  /* The wrapper is only a positioner. Without this the empty space above a
   * collapsed toggle would swallow clicks meant for the page. */
  pointer-events: none;
}

.chat-dock[hidden] {
  display: none;
}

.chat-dock > * {
  pointer-events: auto;
}

/* -- collapsed ------------------------------------------------------------ */

.chat-dock-toggle {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  min-height: 44px;
  padding: 0.4rem 0.85rem 0.4rem 0.4rem;
  border: 1px solid var(--line, #dce3dc);
  border-radius: 999px;
  background: color-mix(in srgb, var(--card, #fff) 92%, transparent);
  box-shadow: 0 12px 34px rgba(15, 23, 42, 0.14);
  backdrop-filter: blur(14px) saturate(140%);
  -webkit-backdrop-filter: blur(14px) saturate(140%);
  color: var(--ink, #17231e);
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}

.chat-dock-toggle:hover {
  border-color: #9fb5a7;
}

.chat-dock-toggle .brand-mark {
  width: 28px;
  height: 28px;
  border-radius: 9px;
  font-size: 13px;
  font-weight: 800;
}

.chat-dock-toggle-label {
  max-width: 12rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* -- open ----------------------------------------------------------------- */

.chat-dock-panel {
  display: flex;
  flex-direction: column;
  width: min(23.5rem, calc(100vw - 2rem));
  /* Short enough that the screen underneath is still readable above it. */
  height: min(60vh, 32rem);
  border: 1px solid var(--line, #dce3dc);
  border-radius: 18px;
  background: var(--card, #fff);
  box-shadow: 0 22px 60px rgba(15, 23, 42, 0.2);
  overflow: hidden;
}

.chat-dock-panel[hidden] {
  display: none;
}

.chat-dock-bar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.85rem 0.95rem;
  border-bottom: 1px solid var(--line, #dce3dc);
  background: #fbfcfa;
}

.chat-dock-heading .eyebrow {
  margin: 0 0 0.2rem;
  font-size: 10px;
  letter-spacing: 0.12em;
}

.chat-dock-heading h2 {
  margin: 0;
  font-size: 15px;
  font-weight: 750;
  letter-spacing: -0.01em;
}

.chat-dock-bar-actions {
  display: flex;
  align-items: center;
  gap: 0.35rem;
}

.chat-dock-plain {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  min-height: 32px;
  padding: 0 0.55rem;
  border: 1px solid var(--line, #dce3dc);
  border-radius: 9px;
  background: #fff;
  color: var(--muted, #68736d);
  font: inherit;
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
}

.chat-dock-plain:hover {
  color: var(--green, #1b6b4b);
  border-color: #9fb5a7;
}

.chat-dock-key {
  padding: 1px 4px;
  border: 1px solid var(--line, #dce3dc);
  border-bottom-width: 2px;
  border-radius: 5px;
  font-size: 9px;
  font-weight: 800;
}

.chat-dock-messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 0.9rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.chat-dock-greeting {
  margin: auto 0;
  color: var(--muted, #68736d);
}

.chat-dock-greeting p {
  margin: 0 0 0.35rem;
  color: var(--ink, #17231e);
  font-size: 15px;
  font-weight: 700;
}

.chat-dock-greeting small {
  font-size: 12px;
  line-height: 1.5;
}

/* The shared renderer draws the same markup the full chat does, and the chat's
 * own stylesheet already colours it — bubbles, the assistant mark, cards. Only
 * what a narrower column needs is restated here.
 *
 * Restating MORE than that is how the first version broke: it re-declared the
 * user bubble's background without its colour, which left white text on a pale
 * green bubble. Inheriting is not laziness here, it is the only way the two
 * chats stay the same chat. */
.chat-dock-messages .chat-message {
  align-items: flex-start;
  max-width: 92%;
}

.chat-dock-messages .message-bubble {
  min-width: 0;
  padding: 0.5rem 0.7rem;
  font-size: 13.5px;
}

.chat-dock-messages .brand-mark {
  flex: 0 0 24px;
  width: 24px;
  height: 24px;
  border-radius: 8px;
  font-size: 12px;
}

/* `.chat-thinking` and `.chat-error` bring their own look from the chat's
 * stylesheet; these only reset the full screen's wider gutters. */
.chat-dock-thinking {
  padding: 0 0.9rem 0.5rem;
}

.chat-dock-error {
  min-height: 0;
  margin: 0;
  padding: 0 0.9rem;
}

.chat-dock-error:empty {
  display: none;
}

.chat-dock-input {
  display: flex;
  align-items: flex-end;
  gap: 0.45rem;
  padding: 0.6rem 0.9rem 0.35rem;
  border-top: 1px solid var(--line, #dce3dc);
}

.chat-dock-input textarea {
  flex: 1;
  min-height: 40px;
  max-height: 92px;
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--line, #dce3dc);
  border-radius: 12px;
  background: #fff;
  color: var(--ink, #17231e);
  font: inherit;
  font-size: 14px;
  line-height: 1.45;
  resize: none;
}

.chat-dock-input textarea:focus {
  border-color: var(--green, #1b6b4b);
  outline: 3px solid rgba(27, 107, 75, 0.1);
}

.chat-dock-input button {
  flex: 0 0 auto;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 999px;
  background: var(--green, #1b6b4b);
  color: #fff;
  font: inherit;
  font-size: 17px;
  cursor: pointer;
}

.chat-dock-input button:disabled {
  opacity: 0.45;
  cursor: default;
}

.chat-dock-privacy {
  margin: 0;
  padding: 0 0.9rem 0.7rem;
  color: var(--muted, #68736d);
  font-size: 10px;
}

/* -- phones --------------------------------------------------------------- */

@media (max-width: 700px) {
  .chat-dock {
    left: max(0.6rem, env(safe-area-inset-left));
    right: max(0.6rem, env(safe-area-inset-right));
    /* Clear of the home button, which owns the bottom centre. */
    bottom: calc(3.4rem + max(0.65rem, env(safe-area-inset-bottom)));
    align-items: stretch;
  }

  .chat-dock-toggle {
    align-self: flex-end;
  }

  .chat-dock-panel {
    width: 100%;
    /* Deliberately short of half the screen so the page it is asking about is
     * still on screen above it. A 375px phone keeps roughly the top 40%. */
    height: min(58vh, 26rem);
  }

  /* The soft keyboard shrinks the visual viewport; app.js already publishes
   * how much, so the panel rides above it rather than under it. */
  .chat-dock[data-open='1'] {
    bottom: calc(3.4rem + max(0.65rem, env(safe-area-inset-bottom)) + var(--keyboard-inset, 0px));
  }
}

@media (prefers-reduced-motion: reduce) {
  .chat-dock-toggle,
  .chat-dock-panel {
    transition: none;
  }
}
