/* AI assistant page (#/chat) — two-pane chat layout. All selectors are prefixed with .chat- */

.chat-page {
  --chat-sidebar-w: 280px;
  --chat-offset: 64px;
  position: relative;
  display: grid;
  grid-template-columns: var(--chat-sidebar-w) minmax(0, 1fr);
  height: calc(100vh - var(--chat-offset));
  height: calc(100dvh - var(--chat-offset));
  min-height: 460px;
  border: 1px solid var(--border);
  border-radius: var(--radius, 12px);
  background: var(--panel);
  box-shadow: var(--shadow);
  overflow: hidden;
}

/* ---------- Sidebar ---------- */

.chat-sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-right: 1px solid var(--border);
  background: var(--panel-2);
}

.chat-sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 14px 14px 10px;
}

.chat-sidebar-title {
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.chat-list {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 4px 8px 12px;
  overflow-y: auto;
}

.chat-item {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 2px;
  width: 100%;
  padding: 9px 10px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.chat-item:hover {
  background: color-mix(in srgb, var(--text) 6%, transparent);
}

.chat-item:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.chat-item.active {
  background: color-mix(in srgb, var(--accent) 14%, transparent);
  border-color: color-mix(in srgb, var(--accent) 35%, transparent);
}

.chat-item-title {
  font-size: 0.92rem;
  font-weight: 500;
}

.chat-item-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.75rem;
}

/* Who started the chat — only visible for people who see more than their own chats. */
.chat-item-actor {
  font-size: 0.72rem;
  opacity: 0.85;
}

.chat-item-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  animation: chat-pulse 1.2s ease-in-out infinite;
}

.chat-list-empty {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
  padding: 12px 8px;
  line-height: 1.5;
}

.chat-list-skeleton .skeleton {
  height: 42px;
  margin: 6px 0;
  border-radius: 8px;
}

.chat-backdrop {
  display: none;
}

/* ---------- Conversation ---------- */

.chat-main {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.chat-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--border);
}

.chat-list-toggle {
  display: none;
}

.chat-head-text {
  flex: 1;
  min-width: 0;
}

.chat-title {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 600;
}

.chat-subtitle:empty {
  display: none;
}

.chat-head-actions {
  display: flex;
  gap: 4px;
}

.chat-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.chat-thread {
  display: flex;
  flex-direction: column;
  gap: 18px;
  max-width: 860px;
  margin: 0 auto;
  padding: 24px 20px 12px;
}

.chat-thread-loading {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.chat-skel-user {
  align-self: flex-end;
  width: 45%;
  height: 40px;
  border-radius: 14px;
}

.chat-skel-ai {
  width: 80%;
  height: 72px;
  border-radius: 12px;
}

.chat-skel-ai.short {
  width: 55%;
  height: 40px;
}

.chat-thread-error {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.chat-msg {
  display: flex;
  gap: 12px;
  animation: chat-in 0.18s ease-out;
}

.chat-msg-user {
  justify-content: flex-end;
}

.chat-bubble {
  max-width: min(640px, 85%);
  padding: 10px 14px;
  border: 1px solid color-mix(in srgb, var(--accent) 35%, transparent);
  border-radius: 16px 16px 4px 16px;
  background: color-mix(in srgb, var(--accent) 16%, var(--panel-2));
  color: var(--text);
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.chat-avatar {
  display: grid;
  flex: none;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), color-mix(in srgb, var(--accent) 55%, #000));
  color: var(--accent-contrast);
  font-size: 15px;
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
}

.chat-msg-body {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 10px;
  min-width: 0;
  padding-top: 5px;
}

.chat-md {
  line-height: 1.6;
  overflow-wrap: anywhere;
}

.chat-md > :first-child {
  margin-top: 0;
}

.chat-md > :last-child {
  margin-bottom: 0;
}

.chat-md pre {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  overflow-x: auto;
}

.chat-md code {
  font-family: var(--mono);
  font-size: 0.88em;
}

.chat-md :not(pre) > code {
  padding: 1px 5px;
  border-radius: 4px;
  background: color-mix(in srgb, var(--text) 10%, transparent);
}

.chat-md blockquote {
  margin: 8px 0;
  padding-left: 12px;
  border-left: 3px solid var(--border);
  color: var(--text-muted);
}

.chat-md ul,
.chat-md ol {
  padding-left: 22px;
}

.chat-md a {
  color: var(--accent);
}

.chat-error {
  margin: 0;
  white-space: pre-wrap;
}

.chat-stopped {
  font-style: italic;
}

.chat-msg-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 26px;
  opacity: 0;
  transition: opacity 0.15s;
}

.chat-msg-ai:hover .chat-msg-meta,
.chat-msg-meta:focus-within {
  opacity: 1;
}

@media (hover: none) {
  .chat-msg-meta {
    opacity: 1;
  }
}

/* ---------- Tool chips ---------- */

.chat-tools {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chat-tool {
  max-width: 100%;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--panel-2);
  font-size: 0.82rem;
  transition: border-color 0.15s, background 0.15s;
}

.chat-tool > summary {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 11px 4px 8px;
  list-style: none;
  cursor: pointer;
  user-select: none;
}

.chat-tool > summary::-webkit-details-marker {
  display: none;
}

.chat-tool > summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 999px;
}

.chat-tool[open] {
  flex-basis: 100%;
  border-radius: 12px;
}

.chat-tool-label {
  font-weight: 500;
}

.chat-tool.is-running {
  border-color: color-mix(in srgb, var(--info) 45%, var(--border));
}

.chat-tool.is-running .chat-tool-icon {
  animation: chat-pulse 1.2s ease-in-out infinite;
}

.chat-tool.is-awaiting {
  border-color: var(--warning);
  background: color-mix(in srgb, var(--warning) 12%, var(--panel-2));
}

.chat-tool.is-ok {
  border-color: color-mix(in srgb, var(--success) 40%, var(--border));
}

.chat-tool.is-error {
  border-color: color-mix(in srgb, var(--danger) 55%, var(--border));
  background: color-mix(in srgb, var(--danger) 8%, var(--panel-2));
}

.chat-tool.is-rejected {
  opacity: 0.8;
}

.chat-tool-detail {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px 12px 10px;
  border-top: 1px solid var(--border);
}

.chat-tool-row {
  display: flex;
  gap: 10px;
  font-size: 0.82rem;
}

.chat-tool-row > :first-child {
  flex: none;
  min-width: 64px;
}

.chat-tool-row.col {
  flex-direction: column;
  gap: 4px;
}

.chat-tool-summary {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.chat-tool-input {
  max-height: 240px;
  margin: 0;
  padding: 8px 10px;
  border-radius: 6px;
  background: var(--bg);
  font-size: 0.78rem;
  white-space: pre;
  overflow: auto;
}

/* ---------- Confirmation card ---------- */

.chat-confirm {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px 16px;
  border: 1px solid var(--warning);
  border-radius: 12px;
  background: linear-gradient(180deg, color-mix(in srgb, var(--warning) 12%, var(--panel)), var(--panel));
  box-shadow: var(--shadow);
  animation: chat-in 0.2s ease-out;
}

.chat-confirm-head {
  display: flex;
  align-items: center;
  gap: 12px;
}

.chat-confirm-icon {
  font-size: 22px;
}

.chat-confirm-title {
  font-weight: 700;
}

.chat-confirm-label {
  color: var(--text-muted);
  font-size: 0.88rem;
}

.chat-confirm-summary {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  font-size: 0.9rem;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.chat-confirm-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.chat-confirm-decision {
  font-weight: 600;
}

/* ---------- Typing indicator ---------- */

.chat-typing {
  display: inline-flex;
  gap: 4px;
  padding: 8px 0;
}

.chat-typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: chat-bounce 1.2s ease-in-out infinite;
}

.chat-typing span:nth-child(2) {
  animation-delay: 0.15s;
}

.chat-typing span:nth-child(3) {
  animation-delay: 0.3s;
}

/* ---------- Empty state ---------- */

.chat-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 32px 8px 16px;
  text-align: center;
}

.chat-empty-mark {
  display: grid;
  place-items: center;
  width: 56px;
  height: 56px;
  border-radius: 16px;
  background: linear-gradient(135deg, var(--accent), color-mix(in srgb, var(--accent) 50%, #000));
  color: var(--accent-contrast);
  font-size: 26px;
  box-shadow: 0 8px 24px color-mix(in srgb, var(--accent) 30%, transparent);
}

.chat-empty-title {
  margin: 8px 0 0;
  font-size: 1.4rem;
}

.chat-empty-text {
  max-width: 580px;
  margin: 0;
  line-height: 1.55;
}

.chat-examples {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  gap: 10px;
  width: 100%;
  margin-top: 14px;
}

.chat-example {
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: auto auto;
  column-gap: 10px;
  row-gap: 4px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel-2);
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
  transition: transform 0.12s, border-color 0.15s, background 0.15s;
}

.chat-example:hover {
  border-color: color-mix(in srgb, var(--accent) 60%, var(--border));
  background: color-mix(in srgb, var(--accent) 6%, var(--panel-2));
  transform: translateY(-1px);
}

.chat-example:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.chat-example-icon {
  grid-row: span 2;
  font-size: 20px;
  line-height: 1.2;
}

.chat-example-title {
  font-size: 0.92rem;
  font-weight: 600;
}

.chat-example-text {
  display: -webkit-box;
  overflow: hidden;
  color: var(--text-muted);
  font-size: 0.82rem;
  line-height: 1.45;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
}

.chat-empty-tip {
  margin: 10px 0 0;
}

/* ---------- Notices & composer ---------- */

.chat-notice {
  width: 100%;
  max-width: 896px;
  margin: 0 auto;
  padding: 0 18px;
}

.chat-notice:empty {
  display: none;
}

.chat-running-notice {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 10px 0 0;
}

.chat-running-notice .btn {
  margin-left: auto;
}

.chat-ai-off {
  margin: 10px 0 0;
  border-color: color-mix(in srgb, var(--warning) 50%, var(--border));
}

.chat-ai-off-head {
  display: flex;
  gap: 12px;
}

.chat-ai-off-head p {
  margin: 4px 0 0;
}

.chat-ai-off-icon {
  font-size: 24px;
}

.chat-ai-off-title {
  font-weight: 700;
}

.chat-ai-off-code {
  margin: 10px 0;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  overflow-x: auto;
}

.chat-ai-off-foot {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.chat-composer {
  padding: 10px 18px 14px;
  border-top: 1px solid var(--border);
  background: var(--panel);
}

.chat-input-wrap {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: 860px;
  margin: 0 auto;
  padding: 6px 6px 6px 14px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--bg-elev);
  transition: border-color 0.15s, box-shadow 0.15s;
}

.chat-input-wrap:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent);
}

.chat-page textarea.chat-input {
  flex: 1;
  min-width: 0;
  min-height: 24px;
  max-height: 220px;
  margin: 0;
  padding: 7px 0;
  border: 0;
  border-radius: 0;
  outline: none;
  background: transparent;
  box-shadow: none;
  color: var(--text);
  font: inherit;
  line-height: 1.5;
  resize: none;
  overflow-y: hidden;
}

.chat-page textarea.chat-input:disabled {
  cursor: not-allowed;
  opacity: 0.6;
}

.chat-send {
  flex: none;
  width: 38px;
  height: 38px;
  padding: 0;
  border-radius: 10px;
}

.chat-stop {
  flex: none;
  height: 38px;
  border-radius: 10px;
}

.chat-hint {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
  max-width: 860px;
  margin: 6px auto 0;
  font-size: 0.75rem;
}

.chat-jump {
  position: absolute;
  bottom: 112px;
  left: 50%;
  z-index: 2;
  border-radius: 999px;
  box-shadow: var(--shadow);
  transform: translateX(-50%);
}

/* ---------- Images: attach button, thumbnails, drop zone, bubble ---------- */

/* The paperclip sits inside the input frame, so the left padding moves to the button. */
.chat-input-wrap {
  padding-left: 6px;
}

.chat-attach {
  flex: none;
  width: 38px;
  height: 38px;
  padding: 0;
  border-radius: 10px;
  font-size: 17px;
  line-height: 1;
}

.chat-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  max-width: 860px;
  margin: 0 auto 8px;
}

.chat-attachment {
  display: flex;
  align-items: center;
  gap: 8px;
  max-width: 240px;
  padding: 5px 5px 5px 6px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-elev);
}

.chat-attachment.is-busy {
  opacity: 0.8;
}

.chat-attachment-thumb {
  position: relative;
  display: grid;
  flex: none;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 7px;
  background: var(--panel-2);
  overflow: hidden;
}

.chat-attachment-img {
  width: 40px;
  height: 40px;
  object-fit: cover;
}

.chat-attachment-img.is-blank {
  display: grid;
  place-items: center;
  font-size: 18px;
}

.chat-attachment-busy {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: color-mix(in srgb, var(--bg) 60%, transparent);
}

.chat-attachment-name {
  min-width: 0;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.chat-attachment-remove {
  flex: none;
}

.chat-msg-user-stack {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
  min-width: 0;
  max-width: min(640px, 85%);
}

.chat-msg-user-stack .chat-bubble {
  max-width: 100%;
}

.chat-bubble-images {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 6px;
}

.chat-image {
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel-2);
  cursor: zoom-in;
  overflow: hidden;
  line-height: 0;
}

.chat-image:hover {
  border-color: var(--accent);
}

.chat-image:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.chat-image img {
  display: block;
  width: 132px;
  height: 132px;
  object-fit: cover;
}

.chat-lightbox {
  display: block;
  max-width: 100%;
  max-height: 68vh;
  margin: 0 auto;
  border-radius: 8px;
}

.chat-drop {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: grid;
  place-items: center;
  padding: 20px;
  background: color-mix(in srgb, var(--bg) 78%, transparent);
  pointer-events: none;
}

.chat-drop-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 26px 32px;
  border: 2px dashed var(--accent);
  border-radius: 16px;
  background: var(--panel);
  text-align: center;
  box-shadow: var(--shadow);
}

.chat-drop-icon {
  font-size: 30px;
  line-height: 1;
}

.chat-drop-title {
  font-weight: 650;
}

.chat-drop-text {
  font-size: 0.8rem;
  color: var(--text-muted);
}

/* ---------- Animations ---------- */

@keyframes chat-in {
  from {
    opacity: 0;
    transform: translateY(4px);
  }

  to {
    opacity: 1;
    transform: none;
  }
}

@keyframes chat-pulse {
  0%,
  100% {
    opacity: 1;
  }

  50% {
    opacity: 0.35;
  }
}

@keyframes chat-bounce {
  0%,
  80%,
  100% {
    opacity: 0.4;
    transform: translateY(0);
  }

  40% {
    opacity: 1;
    transform: translateY(-4px);
  }
}

@media (prefers-reduced-motion: reduce) {
  .chat-msg,
  .chat-confirm,
  .chat-item-dot,
  .chat-typing span,
  .chat-tool.is-running .chat-tool-icon {
    animation: none;
  }

  .chat-sidebar {
    transition: none;
  }
}

/* ---------- Responsive ---------- */

@media (max-width: 900px) {
  .chat-page {
    --chat-offset: 120px;
    grid-template-columns: minmax(0, 1fr);
  }

  .chat-sidebar {
    position: absolute;
    inset: 0 auto 0 0;
    z-index: 5;
    width: min(300px, 85%);
    box-shadow: var(--shadow);
    transform: translateX(-102%);
    transition: transform 0.2s ease;
  }

  .chat-page.list-open .chat-sidebar {
    transform: none;
  }

  .chat-backdrop {
    position: absolute;
    inset: 0;
    z-index: 4;
    display: block;
    background: rgba(0, 0, 0, 0.45);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
  }

  .chat-page.list-open .chat-backdrop {
    opacity: 1;
    pointer-events: auto;
  }

  .chat-list-toggle {
    display: inline-flex;
  }

  .chat-btn-text {
    display: none;
  }

  .chat-thread {
    padding: 16px 12px 8px;
  }

  .chat-composer {
    padding: 8px 10px 10px;
  }

  .chat-notice {
    padding: 0 10px;
  }

  .chat-hint-sep,
  .chat-hint > span:last-child {
    display: none;
  }
}

@media (max-width: 560px) {
  .chat-page {
    border-right: 0;
    border-left: 0;
    border-radius: 0;
  }

  .chat-bubble {
    max-width: 92%;
  }

  .chat-examples {
    grid-template-columns: 1fr;
  }

  .chat-empty-title {
    font-size: 1.2rem;
  }
}

@media (max-width: 560px) {
  .chat-msg-user-stack {
    max-width: 92%;
  }

  .chat-image img {
    width: 104px;
    height: 104px;
  }

  .chat-attachment {
    max-width: 100%;
  }
}
