/* Chat page — full-viewport two-pane layout (TASK_029 v2 Phase 1).
 * Aside (conversation list) + main (stream + composer), sticky composer
 * at the bottom, message stream scrolls inside its own column.
 */

.page--chat {
  /* Override the column default — chat takes the full viewport so the
   * stream can manage its own scroll without the page scrolling. */
  height: 100vh;
  height: 100dvh;
}

.page--chat .page__header {
  /* Header stays compact; everything below is the chat layout. */
  flex: 0 0 auto;
}

.chat-layout {
  flex: 1;
  display: grid;
  grid-template-columns: 280px 1fr;
  gap: 0;
  max-width: var(--container-max);
  width: 100%;
  margin: 0 auto;
  padding: var(--space-5);
  min-height: 0; /* allow children to shrink and scroll */
}

@media (max-width: 720px) {
  .chat-layout {
    grid-template-columns: 1fr;
    padding: var(--space-3);
  }
}

/* ── Sidebar ──────────────────────────────────────────────────────── */

.chat-sidebar {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  display: flex;
  flex-direction: column;
  min-height: 0;
  margin-right: var(--space-4);
  overflow: hidden;
}

@media (max-width: 720px) {
  .chat-sidebar {
    margin-right: 0;
    margin-bottom: var(--space-3);
    max-height: 35vh;
  }
}

.chat-sidebar__head {
  padding: var(--space-4);
  border-bottom: 1px solid var(--color-border);
}

.chat-newbtn {
  width: 100%;
}

.chat-conv-list {
  list-style: none;
  margin: 0;
  padding: var(--space-2);
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}

.chat-conv-list li {
  margin: 0;
}

.chat-conv-row {
  display: flex;
  align-items: center;
  border-radius: var(--radius-md);
  transition: background var(--duration-fast) var(--ease-soft);
}

.chat-conv-row:hover {
  background: var(--color-bg-soft);
}

.chat-conv-row.is-active {
  background: var(--color-bg-soft);
}

.chat-conv-row.is-active .chat-conv-item__title {
  color: var(--color-primary);
  font-weight: 600;
}

.chat-conv-item {
  flex: 1;
  min-width: 0;
  display: block;
  text-align: left;
  background: transparent;
  border: 0;
  padding: var(--space-3) var(--space-3);
  border-radius: var(--radius-md);
  color: var(--color-text);
  font-family: var(--font-body);
  font-size: 0.95rem;
  line-height: 1.3;
  cursor: pointer;
}

.chat-conv-item__title {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-conv-item__meta {
  display: block;
  font-size: 0.78rem;
  color: var(--color-muted);
  margin-top: 2px;
}

/* Hover-revealed rename / delete controls. */
.chat-conv-actions {
  display: flex;
  align-items: center;
  gap: 2px;
  flex: 0 0 auto;
  padding-right: var(--space-2);
  opacity: 0;
  transition: opacity var(--duration-fast) var(--ease-soft);
}

.chat-conv-row:hover .chat-conv-actions,
.chat-conv-row:focus-within .chat-conv-actions {
  opacity: 1;
}

/* Touch devices have no hover — keep the controls reachable. */
@media (hover: none) {
  .chat-conv-actions { opacity: 1; }
}

.chat-conv-action {
  background: transparent;
  border: 0;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  line-height: 1;
  color: var(--color-muted);
}

.chat-conv-action:hover {
  background: var(--color-surface);
  color: var(--color-text);
}

/* Inline rename input replaces the row button while editing. */
.chat-conv-rename {
  flex: 1;
  min-width: 0;
  margin: var(--space-2);
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--color-primary-soft);
  border-radius: var(--radius-md);
  font-family: var(--font-body);
  font-size: 0.95rem;
  line-height: 1.3;
  color: var(--color-text);
  background: var(--color-bg);
}

.chat-conv-rename:focus {
  outline: 2px solid var(--color-primary-soft);
  outline-offset: 1px;
}

/* ── Main pane ────────────────────────────────────────────────────── */

.chat-main {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.chat-main__head {
  padding: var(--space-4) var(--space-5);
  border-bottom: 1px solid var(--color-border);
  flex: 0 0 auto;
}

.chat-title {
  font-size: 1.15rem;
  font-weight: 600;
  margin: 0;
  color: var(--color-primary);
  font-family: var(--font-display);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── Message stream ───────────────────────────────────────────────── */

.chat-stream {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-5);
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  min-height: 0;
}

.chat-msg {
  max-width: 78%;
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-lg);
  font-size: 1rem;
  line-height: 1.55;
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.chat-msg--user {
  align-self: flex-end;
  background: var(--color-primary);
  color: #fff;
  border-bottom-right-radius: var(--radius-sm);
}

.chat-msg--user a {
  color: #fff;
  text-decoration: underline;
}

.chat-msg--assistant {
  align-self: flex-start;
  background: var(--color-bg-soft);
  color: var(--color-text);
  border-bottom-left-radius: var(--radius-sm);
}

.chat-msg__text {
  /* preserves newlines naturally because parent has white-space: pre-wrap */
}

.chat-msg--streaming::after {
  content: '▍';
  display: inline-block;
  color: var(--color-primary-soft);
  animation: chat-caret 1s steps(2) infinite;
  margin-left: 2px;
}

@keyframes chat-caret {
  to { opacity: 0; }
}

/* Thinking accordion — collapsed by default per D3 */
.chat-thinking {
  margin: 0 0 var(--space-3);
  background: rgba(44, 95, 141, 0.05);
  border-left: 3px solid var(--color-primary-soft);
  border-radius: var(--radius-sm);
  padding: var(--space-2) var(--space-3);
  font-size: 0.9rem;
  color: var(--color-muted);
}

.chat-thinking__summary {
  cursor: pointer;
  font-style: italic;
  font-size: 0.85rem;
  color: var(--color-primary-soft);
  user-select: none;
  list-style: none;
}

.chat-thinking__summary::marker {
  content: '';
}

.chat-thinking__summary::-webkit-details-marker {
  display: none;
}

.chat-thinking__summary::before {
  content: '▸ ';
  display: inline-block;
  transition: transform var(--duration-fast) var(--ease-soft);
}

.chat-thinking[open] .chat-thinking__summary::before {
  transform: rotate(90deg);
}

.chat-thinking__body {
  margin-top: var(--space-2);
  white-space: pre-wrap;
  font-family: var(--font-mono);
  font-size: 0.85rem;
  line-height: 1.5;
  color: var(--color-muted);
}

.chat-empty {
  margin: auto;
  text-align: center;
  color: var(--color-muted);
  font-style: italic;
}

.chat-error {
  align-self: stretch;
  background: rgba(181, 75, 75, 0.08);
  border: 1px solid var(--color-error);
  color: var(--color-error);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-md);
  font-size: 0.95rem;
}

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

.chat-composer {
  border-top: 1px solid var(--color-border);
  padding: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  background: var(--color-surface);
  flex: 0 0 auto;
}

.chat-composer__input {
  width: 100%;
  resize: vertical;
  min-height: 64px;
  max-height: 240px;
  padding: var(--space-3);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  font-family: var(--font-body);
  font-size: 1rem;
  line-height: 1.5;
  color: var(--color-text);
  background: var(--color-bg);
}

.chat-composer__input:focus {
  outline: 2px solid var(--color-primary-soft);
  outline-offset: 1px;
  border-color: var(--color-primary-soft);
}

.chat-composer__input:disabled {
  background: var(--color-bg-soft);
  cursor: not-allowed;
  opacity: 0.7;
}

.chat-composer__row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
}

.chat-composer__hint {
  font-size: 0.82rem;
  color: var(--color-muted);
}

/* .btn sets `display: inline-flex`, which beats the UA `[hidden]` rule — so the
 * hidden attribute alone won't hide Send/Cancel. This restores it (class +
 * attribute specificity outranks plain `.btn`). */
.btn[hidden] {
  display: none;
}

.chat-cancel-btn {
  background: transparent;
  color: var(--color-error);
  border: 1px solid var(--color-error);
}

.chat-cancel-btn:hover {
  background: rgba(181, 75, 75, 0.08);
}

/* Muted "(cancelled)" marker appended to a partial assistant turn. */
.chat-cancelled-note {
  align-self: flex-start;
  font-size: 0.82rem;
  font-style: italic;
  color: var(--color-muted);
  margin-top: calc(-1 * var(--space-3));
}

/* ── conversation-list grouping + badges (TASK_044 D4) ────────────────── */

.chat-conv-grouphead {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-3) var(--space-2);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--color-muted);
}

.chat-conv-grouphead__name {
  font-weight: 600;
}

.chat-conv-grouphead__count {
  margin-left: auto;
  background: var(--color-bg-soft);
  border-radius: var(--radius-sm);
  padding: 0 6px;
  font-size: 0.7rem;
  text-transform: none;
}

.chat-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.25rem;
  height: 1.25rem;
  border-radius: 50%;
  font-size: 0.85rem;
  line-height: 1;
}

.chat-badge.is-neutral {
  opacity: 0.55;
}

.chat-conv-imported {
  color: var(--color-primary-soft);
  font-style: italic;
}

/* Continuity banner shown at the top of an imported conversation's stream
 * (fallback when no archive preview is available). */
.chat-import-note {
  align-self: stretch;
  background: rgba(44, 95, 141, 0.06);
  border: 1px dashed var(--color-primary-soft);
  border-radius: var(--radius-md);
  padding: var(--space-3) var(--space-4);
  font-size: 0.85rem;
  font-style: italic;
  color: var(--color-muted);
  text-align: center;
}

/* ── Imported-chat archive preview (GH #52) ───────────────────────────── */

/* Header label above the read-only recent-history preview. */
.chat-archive-head {
  align-self: stretch;
  text-align: center;
  font-size: 0.8rem;
  font-style: italic;
  color: var(--color-muted);
  padding-bottom: var(--space-2);
  border-bottom: 1px dashed var(--color-border);
}

.chat-archive-head a {
  color: var(--color-primary-soft);
}

/* Recent claude.ai messages rendered read-only — dimmed so they read as
 * context, not live turns. Applies on top of .chat-msg--user/--assistant. */
.chat-msg--archive {
  opacity: 0.55;
}

/* Divider between the archived preview and the live continuation. */
.chat-archive-divider {
  align-self: center;
  font-size: 0.78rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--color-muted);
  margin: var(--space-2) 0;
}

/* ── New-chat Kai picker modal (TASK_044 D4) ──────────────────────────── */

.chat-modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
  background: rgba(0, 0, 0, 0.45);
}

/* `display: flex` above beats the UA `[hidden]` rule (which is `display: none`),
 * so the attribute alone wouldn't hide the modal — restore it explicitly. Same
 * pattern as `.btn[hidden]` above. */
.chat-modal[hidden] {
  display: none;
}

.chat-modal__panel {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  width: 100%;
  max-width: 460px;
  max-height: 85vh;
  overflow-y: auto;
  padding: var(--space-5);
  box-shadow: var(--shadow-lg, 0 12px 40px rgba(0, 0, 0, 0.25));
}

.chat-modal__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-3);
}

.chat-modal__title {
  font-family: var(--font-display);
  font-size: 1.2rem;
  color: var(--color-primary);
  margin: 0;
}

.chat-modal__close {
  background: transparent;
  border: 0;
  cursor: pointer;
  font-size: 1.1rem;
  color: var(--color-muted);
  padding: 4px 8px;
  border-radius: var(--radius-sm);
}

.chat-modal__close:hover {
  background: var(--color-bg-soft);
  color: var(--color-text);
}

.chat-modal__error {
  color: var(--color-error);
  font-size: 0.9rem;
  margin: 0 0 var(--space-3);
}

.chat-pick-hint {
  color: var(--color-text);
  margin: 0 0 var(--space-3);
}

.chat-pick-subhint {
  color: var(--color-muted);
  font-size: 0.88rem;
  margin: var(--space-4) 0 var(--space-2);
}

.chat-pick-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: var(--space-3);
}

.chat-pick-tile {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  padding: var(--space-3) var(--space-4);
  background: var(--color-bg-soft);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  cursor: pointer;
  text-align: left;
  transition: border-color var(--duration-fast) var(--ease-soft),
              background var(--duration-fast) var(--ease-soft);
}

.chat-pick-tile:hover {
  border-color: var(--color-primary-soft);
  background: var(--color-surface);
}

.chat-pick-tile__emoji {
  font-size: 1.4rem;
  line-height: 1;
}

.chat-pick-tile__name {
  font-weight: 600;
  color: var(--color-text);
}

.chat-pick-tile__model {
  font-size: 0.75rem;
  color: var(--color-muted);
}

.chat-pick-tile--generic {
  opacity: 0.85;
}

.chat-pick-back {
  background: transparent;
  border: 0;
  cursor: pointer;
  color: var(--color-primary-soft);
  font-size: 0.88rem;
  padding: 0;
  margin-bottom: var(--space-2);
}

.chat-pick-back:hover {
  text-decoration: underline;
}

.chat-pick-fresh {
  width: 100%;
  padding: var(--space-3);
  background: var(--color-primary);
  color: #fff;
  border: 0;
  border-radius: var(--radius-md);
  font-size: 0.95rem;
  cursor: pointer;
}

.chat-pick-fresh:hover {
  filter: brightness(1.05);
}

.chat-pick-convs {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.chat-pick-conv {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: var(--space-3);
  background: var(--color-bg-soft);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  cursor: pointer;
  text-align: left;
}

.chat-pick-conv:hover {
  border-color: var(--color-primary-soft);
  background: var(--color-surface);
}

.chat-pick-conv__title {
  font-size: 0.92rem;
  color: var(--color-text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-pick-conv__meta {
  font-size: 0.75rem;
  color: var(--color-muted);
}
