﻿/* The AI Assistant chat experience - see Components/Pages/Ai/AiChatPanel.razor, shared by the
   dashboard's embedded card and the dedicated /ai/chat page. Deliberately styled to read as
   ChatGPT (centered column, no bubble on the assistant's own reply, rounded pill composer) rather
   than Claude's UI - most of this app's users only know ChatGPT. Also covers
   Admin/AgentSetup.razor's model checklist at the bottom of this file. */

/* A fixed height, never a min-height. The whole point is that the conversation scrolls inside
   this box: with min-height the box grows as messages are added, the page takes the scrollbar, and
   the composer walks off the bottom of the screen - which is exactly what it used to do.

   Full screen gets the viewport minus the app chrome above it; the dashboard's embed gets a fixed
   panel (see [data-embedded]) so it cannot shove the rest of the page down. */
.ai-chat-layout {
  display: flex;
  align-items: stretch;
  height: clamp(420px, calc(100vh - 260px), 900px);
  border: 1px solid var(--color-border-default);
  border-radius: var(--radius-md);
  overflow: hidden;
}

.ai-chat-layout[data-embedded="true"] {
  height: 520px;
}

.ai-chat-sidebar {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 248px;
  flex: 0 0 248px;
  min-height: 0;
  padding: 12px;
  border-right: 1px solid var(--color-border-muted);
  background-color: var(--color-canvas-subtle);
  transition: width 120ms ease, flex-basis 120ms ease;

  /* Collapsed to nothing rather than display:none, so the width animates instead of the
     conversation snapping sideways. overflow:hidden keeps the contents from spilling while it
     narrows; visibility keeps the buttons out of the tab order once it is shut. */
  &[data-collapsed="true"] {
    width: 0;
    flex-basis: 0;
    padding-inline: 0;
    overflow: hidden;
    visibility: hidden;
  }
}

.ai-chat-sidebar-empty {
  padding: 4px 6px;
  font-size: 12.5px;
}

.ai-chat-session-group {
  margin: 10px 0 2px;
  padding-inline: 6px;
  color: var(--color-fg-subtle);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* The row owns hover/active; the button inside it is just the click target, so the rename and
   delete controls can sit alongside without nesting a button inside a button. */
.ai-chat-session-row {
  position: relative;
  display: flex;
  align-items: center;
  border-radius: var(--radius-sm);

  &:hover {
    background-color: var(--color-canvas-inset);
  }

  &[data-active="true"] {
    background-color: var(--color-accent-subtle);
  }

  &:hover .ai-chat-session-actions,
  &:focus-within .ai-chat-session-actions {
    opacity: 1;
  }
}

/* Hidden until the row is hovered or focused - two icons on every row turns a calm history into a
   wall of controls. Focus-within keeps them reachable by keyboard. */
.ai-chat-session-actions {
  display: flex;
  gap: 1px;
  padding-right: 4px;
  opacity: 0;
  transition: opacity 120ms ease;
}

.ai-chat-rename-input {
  width: 100%;
  margin: 2px 4px;
  padding: 5px 7px;
  border: 1px solid var(--color-accent-emphasis);
  border-radius: var(--radius-sm);
  background-color: var(--color-canvas-overlay);
  color: var(--color-fg-default);
  font-size: 12.5px;
  font-weight: 600;
  outline: none;
}

.ai-chat-new-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 12px;
  border: 1px solid var(--color-border-default);
  border-radius: var(--radius-md);
  background-color: var(--color-canvas-overlay);
  color: var(--color-fg-default);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;

  &:hover:not(:disabled) {
    background-color: var(--color-canvas-inset);
  }

  &:disabled {
    opacity: 0.6;
    cursor: default;
  }
}

.ai-chat-sidebar-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

.ai-chat-session-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1;
  min-width: 0;
  padding: 7px 9px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--color-fg-default);
  text-align: left;
  cursor: pointer;

}

.ai-chat-session-title {
  font-size: 12.5px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ai-chat-session-time {
  font-size: 11px;
  color: var(--color-fg-subtle);
}

/* min-height:0 matters as much as min-width here: a flex child defaults to min-height:auto, which
   means it refuses to shrink below its content, and .ai-chat-scroll then never gets a bounded
   height to scroll within. */
.ai-chat-main {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
  min-height: 0;
  background-color: var(--color-canvas-overlay);
}

.ai-chat-topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
  padding: 8px 14px;
  border-bottom: 1px solid var(--color-border-muted);
}

.ai-chat-topbar-title {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
  font-weight: 600;
}

.ai-chat-icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 26px;
  height: 26px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--color-fg-muted);
  font-size: 13px;
  line-height: 1;
  cursor: pointer;

  &:hover:not(:disabled) {
    background-color: var(--color-canvas-inset);
    color: var(--color-fg-default);
  }

  &[data-destructive="true"]:hover:not(:disabled) {
    color: var(--color-danger-fg);
  }

  &:disabled {
    opacity: 0.4;
    cursor: default;
  }
}

.ai-chat-model-pill {
  padding: 4px 10px;
  border: 1px solid var(--color-border-default);
  border-radius: var(--radius-pill);
  background-color: var(--color-canvas-overlay);
  color: var(--color-fg-default);
  font-size: 12px;
}

/* flex:1 + min-height:0 is what makes this the one scrolling element, taking whatever height is
   left between the topbar and the composer. The fixed 60vh height this used to carry fought the
   layout's own height and let the page scroll as well. overscroll-behavior stops a flick at the end
   of the conversation continuing into the page behind it. */
.ai-chat-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.ai-chat-column {
  max-width: 680px;
  margin: 0 auto;
  padding: 20px 20px 8px;
}

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

  & h2 {
    margin: 0;
    font-size: 20px;
  }

  & p {
    max-width: 480px;
    margin: 0;
  }
}

.ai-chat-turn {
  margin-bottom: 18px;

  &[data-role="user"] {
    display: flex;
    justify-content: flex-end;
  }

  &[data-role="assistant"] {
    display: flex;
    align-items: flex-start;
    gap: 10px;
  }
}

.ai-chat-bubble {
  max-width: 80%;
  padding: 9px 13px;
  border-radius: var(--radius-lg);
  background-color: var(--color-canvas-subtle);
  color: var(--color-fg-default);
  font-size: 14px;
  line-height: 1.5;
  white-space: pre-wrap;
}

.ai-chat-avatar {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 26px;
  height: 26px;
  border-radius: var(--radius-pill);
  background-color: var(--color-accent-emphasis);
  color: var(--color-fg-on-emphasis);
  font-size: 13px;
}

.ai-chat-empty .ai-chat-avatar {
  width: 40px;
  height: 40px;
  font-size: 18px;
}

/* No bubble, deliberately - ChatGPT's own answer reads as plain flowing text next to its avatar,
   not a boxed reply, which is what makes a long answer comfortable to read.

   No white-space: pre-wrap any more: the reply is rendered from Markdown into real elements (see
   ChatMarkdown), so paragraphs, lists and tables carry their own spacing. Leaving pre-wrap on would
   double every gap the markup already provides. */
.ai-chat-answer {
  flex: 1;
  min-width: 0;
  padding-top: 3px;
  color: var(--color-fg-default);
  font-size: 14px;
  line-height: 1.6;

  & > *:first-child { margin-top: 0; }
  & > *:last-child { margin-bottom: 0; }

  & p {
    margin: 0 0 10px;
  }

  & h3, & h4, & h5 {
    margin: 14px 0 6px;
    font-size: 14px;
    font-weight: 600;
  }

  & ul, & ol {
    margin: 0 0 10px;
    padding-left: 20px;
  }

  & li {
    margin-bottom: 3px;
  }

  & code {
    padding: 1px 4px;
    border-radius: var(--radius-sm);
    background-color: var(--color-canvas-inset);
    font-family: var(--font-mono);
    font-size: 12.5px;
  }

  & strong {
    font-weight: 600;
  }

  /* A reply's table is data the user will read across - it needs rules and padding, but it is not
     a DataTable and must not borrow that component's chrome. Scrolls on its own so a wide table
     cannot stretch the conversation column. */
  & table {
    display: block;
    width: max-content;
    max-width: 100%;
    margin: 0 0 12px;
    overflow-x: auto;
    border-collapse: collapse;
    font-size: 13px;
  }

  & th, & td {
    padding: 5px 10px;
    border: 1px solid var(--color-border-muted);
    text-align: left;
    white-space: nowrap;
  }

  & th {
    background-color: var(--color-canvas-subtle);
    font-weight: 600;
  }
}

.ai-chat-typing {
  display: flex;
  align-items: center;
  gap: 3px;
  padding-top: 8px;

  & span {
    width: 6px;
    height: 6px;
    border-radius: var(--radius-pill);
    background-color: var(--color-fg-subtle);
    animation: ai-chat-typing-bounce 1.2s infinite ease-in-out;
  }

  & span:nth-child(2) { animation-delay: 0.15s; }
  & span:nth-child(3) { animation-delay: 0.3s; }
}

@keyframes ai-chat-typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30% { transform: translateY(-3px); opacity: 1; }
}

/* Pinned under the scroller by being a flex sibling that does not grow - no sticky or fixed
   positioning needed, and on the dashboard it stays inside the panel rather than floating over the
   page. */
.ai-chat-composer {
  flex-shrink: 0;
  padding: 10px 20px 12px;
  border-top: 1px solid var(--color-border-muted);
  background-color: var(--color-canvas-overlay);
}

.ai-chat-composer .ai-chat-column {
  padding: 0;
}

.ai-chat-disclaimer {
  margin: 6px 0 0;
  color: var(--color-fg-subtle);
  font-size: 11px;
  text-align: center;
}

.ai-chat-suggestions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin-top: 6px;
}

.ai-chat-suggestion {
  padding: 7px 12px;
  border: 1px solid var(--color-border-default);
  border-radius: var(--radius-pill);
  background-color: var(--color-canvas-overlay);
  color: var(--color-fg-default);
  font-size: 12.5px;
  cursor: pointer;

  &:hover:not(:disabled) {
    background-color: var(--color-canvas-inset);
    border-color: var(--color-accent-emphasis);
  }

  &:disabled {
    opacity: 0.5;
    cursor: default;
  }
}

.ai-chat-input-shell {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 8px 8px 16px;
  border: 1px solid var(--color-border-default);
  border-radius: var(--radius-lg);
  background-color: var(--color-canvas-overlay);

  &:focus-within {
    border-color: var(--color-accent-emphasis);
  }
}

.ai-chat-textarea {
  flex: 1;
  resize: none;
  border: 0;
  outline: none;
  background: transparent;
  color: var(--color-fg-default);
  font: inherit;
  font-size: 14px;
  line-height: 1.5;
  padding: 4px 0;

  &::placeholder {
    color: var(--color-fg-subtle);
  }

  &:disabled {
    opacity: 0.6;
  }
}

.ai-chat-send-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: var(--radius-pill);
  background-color: var(--color-accent-emphasis);
  color: var(--color-fg-on-emphasis);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;

  &:hover:not(:disabled) {
    filter: brightness(1.08);
  }

  &:disabled {
    background-color: var(--color-canvas-inset);
    color: var(--color-fg-subtle);
    cursor: default;
  }
}

@media (max-width: 720px) {
  .ai-chat-layout {
    flex-direction: column;
  }

  .ai-chat-sidebar {
    width: 100%;
    flex: 0 0 auto;
    max-height: 160px;
    border-right: 0;
    border-bottom: 1px solid var(--color-border-muted);
  }

  .ai-chat-column {
    padding: 14px 14px 8px;
  }
}

/* ---- Admin/AgentSetup.razor's model checklist - unrelated to the chat layout above ---- */

.ai-model-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
  max-height: 280px;
  overflow-y: auto;
  padding: 8px;
  border: 1px solid var(--color-border-muted);
  border-radius: var(--radius-md);
  background-color: var(--color-canvas-subtle);
}
