/* ── Public chat surface ──────────────────────────────────
   The composer is one rounded surface: pills above, text in the middle, a
   single control row below — “+” menu left, mic / live voice / send right. */

/* Wider column, quieter scrollbar. Dynamic viewport height so the composer
   stays visible on iPad/mobile where browser chrome overlaps 100vh — dvh
   tracks the visible viewport as the tab/URL bar shows and hides. */
.chat-shell {
  max-width: 1040px;
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  overflow: hidden;
  padding-left: max(22px, env(safe-area-inset-left, 0px));
  padding-right: max(22px, env(safe-area-inset-right, 0px));
}

.chat-head { flex: none; }
.chat-head .grow { min-width: 0; }
.chat-title, .chat-subtitle { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-build-cta { flex: none; white-space: nowrap; }

.chat-log {
  min-height: 0;
  padding-right: 14px;
  overscroll-behavior-y: contain;
  scrollbar-gutter: stable;
  scrollbar-width: thin;
  scrollbar-color: rgba(128, 128, 128, 0.22) transparent;
}
.chat-log::-webkit-scrollbar { width: 6px; }
.chat-log::-webkit-scrollbar-track { background: transparent; }
.chat-log::-webkit-scrollbar-thumb { background: rgba(128, 128, 128, 0.18); border-radius: 8px; }
.chat-log::-webkit-scrollbar-thumb:hover { background: rgba(128, 128, 128, 0.34); }

/* ── Composer box ───────────────────────────────────────── */

.composer--box {
  flex-direction: column;
  align-items: stretch;
  gap: 0;
  padding: 10px 12px 26px;
}

.composer { flex: none; }

.composer--box::before { content: none; }

.composer--box .composer-input,
.composer--box .composer-input:focus {
  border: none;
  background: transparent;
  box-shadow: none;
  padding: 10px 6px 6px;
  max-height: 200px;
}

.composer--box {
  border-radius: var(--r-lg);
  border: 1px solid var(--line-hi);
  background: var(--bg-raised);
  transition: border-color 0.18s var(--ease), box-shadow 0.18s var(--ease);
}

.composer--box:focus-within { border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }

.composer-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding-top: 2px;
}

.composer-side { display: inline-flex; align-items: center; gap: 8px; }

.composer-btn {
  width: 36px;
  height: 36px;
  border-radius: var(--r-pill);
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  display: grid;
  place-items: center;
  flex: none;
  transition: color 0.16s var(--ease), background 0.16s var(--ease), border-color 0.16s var(--ease);
}

.composer-btn:hover:not(:disabled) { color: var(--text); background: var(--surface-hi); }
.composer-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.composer-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.composer-btn svg { width: 18px; height: 18px; }
.composer-btn.is-on { color: var(--accent); background: var(--accent-soft); }
.composer-btn.is-recording { color: var(--danger); background: rgba(217, 128, 128, 0.12); }

.composer-error { font-size: 13px; color: var(--danger); padding: 2px 6px 8px; }

/* Recording clock */
.record-clock {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--danger);
}
.record-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--danger);
  animation: pulse 1.4s ease-in-out infinite;
}

/* ── Attachment pills ───────────────────────────────────── */

.attach-row { display: flex; flex-wrap: wrap; gap: 6px; padding: 4px 4px 8px; }

.attach-pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  max-width: 240px;
  padding: 6px 10px;
  border-radius: var(--r-pill);
  border: 1px solid var(--line);
  background: var(--surface);
  font-size: 12.5px;
  color: var(--muted);
}

.attach-pill svg { width: 13px; height: 13px; flex: none; color: var(--accent); }
.attach-pill-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.attach-pill-x {
  border: none; background: none; padding: 0;
  color: var(--subtle); cursor: pointer;
  display: grid; place-items: center; flex: none;
}
.attach-pill-x svg { width: 12px; height: 12px; color: inherit; }
.attach-pill-x:hover { color: var(--danger); }

/* Pills inside a sent user bubble */
.msg-attachments { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.msg--user .attach-pill { border-color: rgba(255, 255, 255, 0.28); background: rgba(255, 255, 255, 0.14); color: inherit; }
.msg--user .attach-pill svg { color: inherit; }

/* ── “+” menu ───────────────────────────────────────────── */

.attach-anchor { position: relative; }

.attach-menu {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 0;
  min-width: 190px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 6px;
  border-radius: var(--r-md);
  border: 1px solid var(--line-hi);
  background: var(--bg-raised);
  box-shadow: var(--shadow-lg);
  z-index: 30;
  animation: rise 0.18s var(--ease);
}

.attach-menu-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 11px;
  border: none;
  border-radius: var(--r-sm);
  background: transparent;
  color: var(--text);
  font-size: 14px;
  text-align: left;
  cursor: pointer;
  transition: background 0.14s var(--ease);
}

.attach-menu-item:hover { background: var(--surface-hi); }
.attach-menu-item svg { width: 16px; height: 16px; color: var(--muted); flex: none; }

.attach-link-row { display: flex; align-items: center; gap: 8px; padding: 0 4px 8px; }
.attach-link-row .input { padding: 9px 13px; font-size: 14px; }

/* ── Chat switcher (header): new chat + previous chats ──── */

.chat-switch { position: relative; display: inline-flex; align-items: center; gap: 2px; }

.chat-switch-menu {
  bottom: auto;
  top: calc(100% + 8px);
  left: auto;
  right: 0;
  min-width: 240px;
  max-width: min(320px, 86vw);
  max-height: 300px;
  overflow-y: auto;
}

.chat-switch-item { justify-content: space-between; gap: 10px; cursor: pointer; user-select: none; -webkit-user-select: none; }
.chat-switch-item[aria-current="true"] { color: var(--accent); }
.chat-switch-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; text-align: left; font-size: 13.5px; }
.chat-switch-when { flex: none; color: var(--subtle); font-size: 11.5px; }
.chat-switch-empty { display: flex; align-items: center; gap: 8px; padding: 12px; font-size: 13px; color: var(--subtle); }

/* Delete: revealed on hover where hover exists; right-click / press-and-hold
   arm the same inline confirm everywhere. */
.chat-switch-del {
  flex: none;
  width: 24px;
  height: 24px;
  border: none;
  border-radius: 7px;
  background: transparent;
  color: var(--subtle);
  cursor: pointer;
  display: grid;
  place-items: center;
  opacity: 0;
  transition: opacity 0.14s var(--ease), color 0.14s var(--ease), background 0.14s var(--ease);
}
.chat-switch-del svg { width: 13px; height: 13px; }
.chat-switch-del:hover { color: var(--danger); background: rgba(217, 128, 128, 0.12); }
@media (hover: hover) {
  .chat-switch-item:hover .chat-switch-del,
  .chat-switch-item:focus-within .chat-switch-del { opacity: 1; }
}

.chat-switch-item.is-confirm { cursor: default; }
.chat-switch-item.is-confirm .chat-switch-title { color: var(--danger); }

.chat-switch-confirm, .chat-switch-cancel {
  flex: none;
  padding: 5px 12px;
  border-radius: var(--r-pill);
  font-size: 12.5px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.chat-switch-confirm {
  border: none;
  background: var(--danger);
  color: #fff;
}
.chat-switch-confirm:disabled { opacity: 0.6; cursor: wait; }
.chat-switch-cancel {
  border: 1px solid var(--line);
  background: transparent;
  color: var(--muted);
}
.chat-switch-cancel:hover { color: var(--text); }
.chat-switch-confirm .spinner { width: 11px; height: 11px; }

/* ── Read-aloud on answers ──────────────────────────────── */

.msg-body { display: flex; flex-direction: column; gap: 4px; min-width: 0; }

.msg-action-area { display: flex; flex-direction: column; align-items: flex-start; gap: 3px; }
.msg-actions { display: flex; gap: 4px; padding-left: 4px; }

.msg-action {
  width: 26px; height: 26px;
  border: none; border-radius: 7px;
  background: transparent;
  color: var(--subtle);
  cursor: pointer;
  display: grid; place-items: center;
  opacity: 0;
  transition: opacity 0.15s var(--ease), color 0.15s var(--ease), background 0.15s var(--ease);
}

.msg--agent:hover .msg-action, .msg-action.is-on, .msg-action:focus-visible { opacity: 1; }
.msg-action:hover { color: var(--text); background: var(--surface-hi); }
.msg-action.is-on { color: var(--accent); }
.msg-action svg { width: 14px; height: 14px; }
.msg-action .spinner { width: 12px; height: 12px; }
.tts-error { padding-left: 4px; color: var(--danger); font-size: 12px; line-height: 1.4; }


/* Devices without dependable hover (phones and iPad) must not hide controls. */
@media (hover: none), (pointer: coarse) {
  .composer-btn, .send-btn { width: 44px; height: 44px; }
  .msg-action { width: 36px; height: 36px; opacity: 1; }
  .attach-pill-x { min-width: 28px; min-height: 28px; margin: -7px -5px -7px 0; }
  .attach-menu-item { min-height: 44px; }
}

/* ── Tablet and Mobile ─────────────────────────────────── */

@media (max-width: 1024px) {
  .chat-shell {
    padding-left: max(16px, env(safe-area-inset-left, 0px));
    padding-right: max(16px, env(safe-area-inset-right, 0px));
  }
  .msg-bubble { max-width: 84%; }
}

@media (max-width: 700px) {
  .chat-shell {
    padding-left: max(10px, env(safe-area-inset-left, 0px));
    padding-right: max(10px, env(safe-area-inset-right, 0px));
  }
  .chat-head { padding: 12px 0 10px; gap: 8px; }
  .chat-avatar { width: 34px; height: 34px; border-radius: 11px; font-size: 14px; }
  /* The name yields before the controls do. */
  .chat-head .grow { min-width: 0; }
  .chat-title { font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .chat-build-cta { padding: 7px 10px; font-size: 12.5px; }
  /* The previous-chats dropdown becomes a full-width sheet under the header. */
  .chat-switch-menu {
    position: fixed;
    top: 62px;
    left: 10px;
    right: 10px;
    min-width: 0;
    max-width: none;
    max-height: 50vh;
  }
  .chat-log { padding: 18px 10px 18px 0; gap: 16px; }
  .composer { padding: 8px 0 max(12px, env(safe-area-inset-bottom, 0px)); }
  .composer--box { padding: 8px 10px; border-radius: 20px; }
  /* 16px stops iOS Safari zooming the page on focus. */
  .composer--box .composer-input { font-size: 16px; }
  .send-btn, .composer-btn { width: 44px; height: 44px; }
  .attach-link-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: stretch;
  }
  .attach-link-row .btn { min-height: 44px; }
  .attach-pill { max-width: min(240px, 100%); }
  /* Touch has no hover: actions stay visible. */
  .msg-action { opacity: 1; }
  .starter-grid { grid-template-columns: 1fr; }
}

@media (max-width: 420px) {
  .chat-build-cta { padding-inline: 10px; font-size: 12px; }
  .chat-head { gap: 8px; }
  .composer-side { gap: 4px; }
  .composer-controls { gap: 4px; }
  .msg-bubble { max-width: 90%; }
}

@media (prefers-reduced-motion: reduce) {
  .record-dot { animation: none; }
}
