:root {
  --bg: #0e1116;
  --panel: #161b22;
  --panel-2: #1f2630;
  --border: #2d333b;
  --text: #e6edf3;
  --dim: #8b949e;
  --accent: #ffb000;
  --user: #2c3a4a;
  --assistant: #1f2630;
  --warn: #d9534f;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
/* Use dynamic viewport units so iOS keyboard doesn't push the composer off-screen. */
@supports (height: 100dvh) {
  html, body { height: 100dvh; }
}
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text",
        "Segoe UI", system-ui, sans-serif;
  display: flex; flex-direction: column;
  /* Respect iOS notch/home-bar safe areas. */
  padding-bottom: env(safe-area-inset-bottom);
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: contain;
}
header.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand .logo {
  background: var(--accent); color: #1a1a1a;
  font-weight: 800; padding: 4px 8px; border-radius: 6px;
  letter-spacing: 0.5px;
}
.meta { display: flex; align-items: center; gap: 10px; }
.pill {
  background: var(--panel-2); border: 1px solid var(--border);
  color: var(--dim); padding: 4px 10px; border-radius: 999px;
  font-size: 12px;
}
.pill.subtle { color: var(--dim); }
.pill.warn { background: #3b1d1f; color: #ffd1d1; border-color: #5b2528; }
.user { color: var(--dim); font-size: 13px; }
.new-chat {
  background: transparent; color: var(--text); border: 1px solid var(--border);
  padding: 5px 12px; border-radius: 999px; cursor: pointer;
  font-size: 13px; font-weight: 500;
  display: inline-flex; align-items: center; gap: 6px;
  line-height: 1;
}
.new-chat:hover { border-color: var(--accent, #f59e0b); color: var(--accent, #f59e0b); }
.new-chat-plus { font-size: 16px; font-weight: 600; line-height: 1; }

main { flex: 1; overflow: hidden; }
.chat {
  height: 100%; overflow-y: auto;
  padding: 24px max(16px, calc(50% - 420px));
  display: flex; flex-direction: column; gap: 18px;
  -webkit-overflow-scrolling: touch;          /* momentum scroll on iOS */
  overscroll-behavior: contain;
}

.history-banner {
  align-self: center;
  font-size: 12px;
  color: var(--dim);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 12px;
  margin-bottom: 4px;
}
.welcome { color: var(--dim); }
.welcome h2 { color: var(--text); margin: 0 0 6px; font-size: 18px; }
.examples { list-style: none; padding: 0; margin: 12px 0; }
.examples li {
  background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 8px; padding: 10px 12px; margin-bottom: 8px; cursor: pointer;
}
.examples li:hover { border-color: var(--accent); }
.examples a { color: var(--text); text-decoration: none; display: block; }

.msg-pair { display: flex; flex-direction: column; gap: 8px; }
.msg { display: flex; }
.msg.user { justify-content: flex-end; }
/* Assistant messages stack the bubble + msg-actions + tool-trace
   vertically so the share/copy buttons land directly under the answer
   instead of pushed off to the side (or off-screen on mobile). */
.msg.assistant { flex-direction: column; align-items: flex-start; }
.bubble {
  max-width: 80%;
  padding: 10px 14px; border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--assistant);
  white-space: pre-wrap; word-wrap: break-word;
  position: relative;          /* anchor for floating action buttons on mobile */
}
/* Inner text container so the bubble can also host non-text children
   (the action buttons) without inheriting white-space: pre-wrap. */
.bubble-text { white-space: pre-wrap; word-wrap: break-word; }
.msg.user .bubble {
  background: var(--user); border-color: #3a4757;
}
.preserve { white-space: pre-wrap; }
/* ---- Share / copy actions on assistant bubbles ----
   Same compact icon stack on both desktop and mobile: two small amber
   squares (matching the Ask button) anchored to the bubble's top-right
   corner. Bubble grows extra right-padding so answer text never runs
   under them. */
.msg.assistant .bubble { padding-right: 50px; }
.msg.assistant .msg-actions {
  position: absolute;
  top: 6px;
  right: 6px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 0;
  z-index: 1;
}
.action {
  width: 32px;
  height: 32px;
  min-height: 32px;
  padding: 0;
  border-radius: 8px;
  background: var(--accent);
  color: #1a1a1a;
  border: 1px solid var(--accent);
  font: inherit;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: filter 120ms, transform 80ms;
}
.action:hover, .action:focus-visible {
  filter: brightness(1.08);
  outline: none;
}
.action:active { transform: scale(0.94); }
.action-icon { font-size: 15px; font-weight: 700; line-height: 1; }
.action-label { display: none; }   /* always icon-only — the title attribute / aria-label handles a11y */
.action:hover, .action:focus-visible {
  color: var(--text);
  border-color: var(--accent);
  outline: none;
}
.action:active { background: var(--panel-2); }
.share-icon { font-size: 13px; line-height: 1; }
/* On browsers without navigator.share, the Share button is pure clutter —
   the Copy button does the same thing. <html.no-webshare> is set in JS. */
.no-webshare .action.share { display: none; }

/* ---- Toast (clipboard / share confirmation) ---- */
#toast {
  position: fixed;
  bottom: max(80px, calc(72px + env(safe-area-inset-bottom)));
  left: 50%;
  transform: translateX(-50%) translateY(8px);
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--accent);
  border-radius: 999px;
  padding: 8px 16px;
  font-size: 13px;
  font-weight: 500;
  pointer-events: none;
  opacity: 0;
  transition: opacity 180ms ease, transform 180ms ease;
  z-index: 100;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
  white-space: nowrap;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.trace {
  margin-left: 4px; font-size: 12px; color: var(--dim);
}
.trace summary { cursor: pointer; }
.trace ol { margin: 6px 0 0 18px; padding: 0; }
.trace code { background: #0b0f14; padding: 1px 5px; border-radius: 4px; }
.trace .dim { margin-left: 6px; opacity: 0.7; }
.trace .err { color: #ff8a8a; margin-left: 6px; }

form.composer {
  display: flex; gap: 8px;
  padding: 12px max(16px, calc(50% - 420px));
  border-top: 1px solid var(--border);
  background: var(--panel);
}
form.composer input {
  flex: 1; padding: 12px 14px; font-size: 15px;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--panel-2); color: var(--text);
}
form.composer input:focus { outline: 2px solid var(--accent); border-color: var(--accent); }
form.composer button {
  padding: 0 18px; border: 0; border-radius: 10px;
  background: var(--accent); color: #1a1a1a; font-weight: 700; cursor: pointer;
}
form.composer button:hover { filter: brightness(1.05); }

/* ---- Loading indicator: a calm three-dot pulse (iMessage-style typing) ---- */
.thinking {
  display: none;
  align-items: center; gap: 12px;
  align-self: flex-start;
  background: var(--assistant);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 12px 16px;
  color: var(--dim);
  font-size: 14px;
}
.thinking[data-show="1"] {
  display: inline-flex;
}

/* Three dots that softly fade in and out in sequence — no rotation, no bounce. */
.dots {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}
.dots i {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--accent);
  display: inline-block;
  opacity: 0.25;
  animation: dot-fade 1.4s infinite ease-in-out;
}
.dots i:nth-child(1) { animation-delay: 0s; }
.dots i:nth-child(2) { animation-delay: 0.18s; }
.dots i:nth-child(3) { animation-delay: 0.36s; }
@keyframes dot-fade {
  0%, 60%, 100% { opacity: 0.25; transform: scale(1); }
  30%           { opacity: 1;    transform: scale(1.15); }
}

@media (prefers-reduced-motion: reduce) {
  .dots i { animation: none; opacity: 0.6; }
}

/* Composer button — swap label while in flight. */
form.composer button#askBtn { position: relative; min-width: 92px; }
.ask-default, .ask-busy { display: inline-flex; align-items: center; gap: 6px; }
.ask-busy { display: none; }
form.composer.htmx-request button#askBtn .ask-default { display: none; }
form.composer.htmx-request button#askBtn .ask-busy    { display: inline-flex; }
form.composer button:disabled,
form.composer input:disabled {
  opacity: 0.85; cursor: default;
}
/* Inline mini-dots inside the Ask button (matches the main indicator). */
.ask-busy .dots i { width: 5px; height: 5px; background: rgba(26,26,26,0.7); }

/* Pending user-message bubble (rendered optimistically before server reply). */
.msg-pair.pending .msg.user .bubble {
  opacity: 0.85;
}

/* ======================================================================
   Mobile (≤720px): roomier touch targets, collapsed header,
   keyboard-safe composer, oversized thinking indicator.
   ====================================================================== */
@media (max-width: 720px) {
  /* Slightly smaller body font wins back room. */
  body { font-size: 14.5px; }

  /* Header — drop the noisy item-count pill and the email; keep brand + data-as-of + reset. */
  header.topbar {
    padding: 8px 12px;
    gap: 8px;
  }
  .meta { gap: 6px; flex-wrap: nowrap; }
  .meta .pill.subtle,
  .meta .user { display: none; }
  /* Sign out stays visible on mobile — typing /logout in the URL bar
     was the only way to log out before, which was non-obvious. */
  .meta .pill.subtle.sign-out { display: inline-flex; }
  .meta .pill { font-size: 11px; padding: 3px 8px; }
  .new-chat { padding: 6px 11px; font-size: 13px; }
  .new-chat-label { display: none; }   /* mobile: just the + */
  .new-chat-plus { font-size: 18px; }

  /* Chat column — full width, comfortable side padding. */
  .chat { padding: 14px 12px 18px; gap: 14px; }
  .bubble { max-width: 92%; padding: 10px 12px; }

  /* Welcome examples — bigger tappable rows. */
  .welcome h2 { font-size: 16px; }
  .examples li { padding: 12px 14px; line-height: 1.4; }
  .examples a { font-size: 14.5px; }

  /* Composer — make sure inputs/buttons hit the 44px touch-target minimum. */
  form.composer {
    padding: 10px 10px calc(10px + env(safe-area-inset-bottom));
    gap: 6px;
  }
  form.composer input {
    padding: 12px 12px;
    font-size: 16px;            /* iOS won't auto-zoom on focus at ≥16px */
    border-radius: 12px;
  }
  form.composer button#askBtn {
    min-width: 72px;
    min-height: 46px;
    padding: 0 14px;
    border-radius: 12px;
    font-size: 15px;
  }

  /* Thinking pill — bigger, still calm. */
  .thinking {
    padding: 14px 16px;
    font-size: 15px;
    gap: 14px;
  }
  .dots i { width: 8px; height: 8px; }

  /* Tool-trace details collapse cleanly. */
  .trace { font-size: 11.5px; }

  /* Slightly bigger touch targets for the action buttons on phone. */
  .action { width: 36px; height: 36px; min-height: 36px; border-radius: 9px; }
  .action-icon { font-size: 16px; }
}

/* Very narrow phones — squeeze the apex header even more. */
@media (max-width: 380px) {
  header.topbar .brand strong { display: none; }   /* keep just the "84" badge */
  .meta .pill { font-size: 10px; }
}

/* Slash-command autocomplete menu (saved prompts).
   Floats above the composer; same dark surface as bubbles. */
.slash-menu {
  position: fixed;
  left: 0; right: 0;
  bottom: 70px;       /* sits above the composer */
  margin: 0 auto;
  max-width: 760px;
  max-height: 320px;
  overflow-y: auto;
  background: #1f242c;
  border: 1px solid #313846;
  border-radius: 10px;
  box-shadow: 0 -4px 24px rgba(0,0,0,.4);
  z-index: 50;
  padding: .35rem 0;
}
.slash-empty { padding: .8rem 1rem; color: #b8c0d0; font-size: 14px; }
.slash-empty a { color: #9ab2ff; }
.slash-item { padding: .55rem .9rem; cursor: pointer; border-left: 3px solid transparent; }
.slash-item.active { background: #2a3142; border-left-color: #ffb43a; }
.slash-item:hover { background: #262d3b; }
.slash-line { display: flex; gap: .55rem; align-items: center; }
.slash-slug { font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
              color: #ffb43a; font-size: 13.5px; }
.slash-badge { font-size: 10.5px; padding: 1px 7px; border-radius: 999px;
               background: #313846; color: #c9d2e4; text-transform: uppercase;
               letter-spacing: .04em; }
.slash-badge.system { background: #4a3a14; color: #ffd69a; }
.slash-title { color: #d8dde6; font-size: 13px; margin-top: 2px;
               white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

@media (max-width: 720px) {
  .slash-menu { left: 8px; right: 8px; bottom: 78px; max-height: 50vh; }
}

.slash-hint {
  padding: .45rem .9rem;
  border-top: 1px solid #313846;
  color: #8b94a8;
  font-size: 11.5px;
  letter-spacing: .02em;
  background: #181d24;
}

/* Profile avatar in topbar — small circle, replaces email pill. */
.profile-link {
  display: inline-flex; align-items: center; justify-content: center;
  width: 32px; height: 32px; border-radius: 50%;
  margin-left: .25rem;
  text-decoration: none;
  background: #2a3142; color: #fff; overflow: hidden;
  border: 1px solid #3a4152;
}
.avatar-sm { width: 100%; height: 100%; display: block; object-fit: cover; }
.avatar-initial {
  display: grid; place-items: center;
  font-size: 13px; font-weight: 600; line-height: 1;
}
