/**
 * ChatContainer Component
 * Displays chat messages in a scrollable container
 * 
 * Dependencies: chat-tokens.css
 */

/* ============================================
   CONTAINER
   ============================================ */

.chat-container {
  max-height: calc(
    100vh - var(--chat-layout-header-height) - var(--chat-layout-footer-height)
  );
  overflow: auto;
  padding: var(--chat-space-lg);
  scroll-behavior: smooth;
}

.chat-container::after {
  content: "";
  display: block;
  height: var(--chat-layout-footer-height);
}

/* ============================================
   BANNERS
   ============================================ */

.readonly-banner {
  background: var(--chat-status-warning-bg);
  border: 1px solid var(--chat-status-warning);
  border-radius: var(--chat-radius-lg);
  padding: var(--chat-space-md) var(--chat-space-lg);
  margin-bottom: var(--chat-space-lg);
  font-size: var(--chat-font-size-md);
  line-height: var(--chat-line-height-base);
  font-weight: var(--chat-font-weight-medium);
  color: var(--chat-status-warning-darkest);
  text-align: center;
}

/* ============================================
   MESSAGE LAYOUT
   ============================================ */

.message-row {
  display: flex;
  align-items: flex-start;
  gap: 0.6rem;
  margin: 0.55rem 0;
  animation: var(--chat-animation-fade-in);
}

.message-row.user {
  justify-content: flex-end;
}

.message-row.assistant {
  justify-content: flex-start;
}

/* ============================================
   AVATAR
   ============================================ */

.avatar {
  width: 36px;
  height: 36px;
  border-radius: var(--chat-radius-round);
  background-size: cover;
  background-position: center;
  flex: 0 0 36px;
  box-shadow: var(--chat-shadow-avatar);
}

/* ============================================
   MESSAGE BUBBLE
   ============================================ */

.message {
  max-width: min(var(--chat-layout-message-max-width), 78vw);
  padding: var(--chat-space-md) 0.9rem;
  border-radius: var(--chat-radius-xl);
  font-size: var(--chat-font-size-md);
  line-height: var(--chat-line-height-relaxed);
  box-shadow: var(--chat-shadow-message);
  word-wrap: break-word;
  overflow-wrap: break-word;
  white-space: normal;
  font-weight: var(--chat-font-weight-normal);
  letter-spacing: 0.01em;
}

.message.assistant {
  color: var(--chat-text-primary);
  background: var(--chat-surface-primary);
  border: 1px solid var(--chat-border-alpha);
}

.message.user {
  color: #ffffff;
  background: var(--chat-brand-primary);
}

.message.error {
  background: var(--chat-status-error-bg);
  color: var(--chat-status-error-dark);
  border: 1px solid var(--chat-status-error-light);
}

/* ============================================
   MESSAGE CONTENT - TABLES
   ============================================ */

.message table {
  width: 100%;
  border-collapse: collapse;
  table-layout: auto;
  margin: var(--chat-space-md) 0;
  font-size: 0.95em;
}

.message th,
.message td {
  border: 1px solid var(--chat-border-primary);
  padding: var(--chat-space-md) var(--chat-space-md);
  vertical-align: top;
  text-align: left;
  line-height: var(--chat-line-height-base);
}

.message th {
  font-weight: var(--chat-font-weight-semibold);
  background: var(--chat-surface-secondary);
}

.message .qpoqqcc-table {
  table-layout: fixed;
}

.message .qpoqqcc-table th:nth-child(1) {
  width: 20%;
}
.message .qpoqqcc-table th:nth-child(2) {
  width: 40%;
}
.message .qpoqqcc-table th:nth-child(3) {
  width: 10%;
}
.message .qpoqqcc-table th:nth-child(4) {
  width: 30%;
}

/* ============================================
   MESSAGE CONTENT - LISTS
   ============================================ */

.message ul,
.message ol {
  margin: var(--chat-space-md) 0;
  padding-left: var(--chat-space-2xl);
  line-height: var(--chat-line-height-relaxed);
}

.message li {
  margin: var(--chat-space-sm) 0;
  line-height: inherit;
}

.message li > p {
  margin: var(--chat-space-xs) 0;
}

.message ul ul,
.message ol ol,
.message ul ol,
.message ol ul {
  margin: var(--chat-space-xs) 0;
}

/* ============================================
   MESSAGE CONTENT - CODE
   ============================================ */

.message code {
  background: var(--chat-surface-tertiary);
  padding: 0.2rem 0.4rem;
  border-radius: var(--chat-radius-sm);
  font-family: var(--chat-font-family-mono);
  font-size: 0.88em;
}

.message pre {
  background: var(--chat-surface-tertiary);
  padding: var(--chat-space-md);
  border-radius: var(--chat-radius-md);
  overflow-x: auto;
  margin: var(--chat-space-md) 0;
  line-height: 1.6;
}

.message pre code {
  background: none;
  padding: 0;
}

/* ============================================
   MESSAGE CONTENT - TEXT ELEMENTS
   ============================================ */

.message p {
  margin: var(--chat-space-sm) 0;
  line-height: inherit;
}

.message p:first-child {
  margin-top: 0;
}

.message p:last-child {
  margin-bottom: 0;
}

.message h1,
.message h2,
.message h3,
.message h4,
.message h5,
.message h6 {
  margin: var(--chat-space-lg) 0 var(--chat-space-md) 0;
  font-weight: var(--chat-font-weight-semibold);
  line-height: var(--chat-line-height-tight);
}

.message h1:first-child,
.message h2:first-child,
.message h3:first-child,
.message h4:first-child,
.message h5:first-child,
.message h6:first-child {
  margin-top: 0;
}

.message h1 {
  font-size: 1.5em;
}

.message h2 {
  font-size: 1.3em;
}

.message h3 {
  font-size: 1.15em;
}

.message h4 {
  font-size: 1.05em;
}

.message h5,
.message h6 {
  font-size: 1em;
}

.message strong,
.message b {
  font-weight: var(--chat-font-weight-semibold);
}

.message em,
.message i {
  font-style: italic;
}

.message blockquote {
  margin: var(--chat-space-md) 0;
  padding-left: var(--chat-space-lg);
  border-left: 3px solid var(--chat-border-primary);
  color: var(--chat-text-secondary);
  font-style: italic;
}

.message a {
  color: var(--chat-brand-primary);
  text-decoration: underline;
  transition: var(--chat-transition-colors);
}

.message a:hover {
  color: var(--chat-brand-primary-hover);
}

.message hr {
  margin: var(--chat-space-lg) 0;
  border: none;
  border-top: 1px solid var(--chat-border-primary);
}

/* ============================================
   FILE ATTACHMENTS
   ============================================ */

.file-attachment {
  display: inline-block;
  background: rgba(0, 0, 0, 0.1);
  padding: 0.4rem 0.6rem;
  border-radius: var(--chat-radius-md);
  margin: var(--chat-space-xs) var(--chat-space-xs) 0 0;
  font-size: var(--chat-font-size-sm);
  line-height: var(--chat-line-height-base);
}

.message.user .file-attachment {
  background: rgba(255, 255, 255, 0.2);
}

/* ============================================
   TYPING INDICATOR
   ============================================ */

.typing-indicator {
  max-width: min(var(--chat-layout-message-max-width), 78vw);
  padding: var(--chat-space-md) 0.9rem;
  background: var(--chat-surface-secondary);
  border-radius: var(--chat-radius-xl);
  color: var(--chat-text-secondary);
  font-size: var(--chat-font-size-md);
  font-style: italic;
  line-height: var(--chat-line-height-base);
  animation: var(--chat-animation-pulse);
  border: 1px solid var(--chat-border-alpha);
  box-shadow: var(--chat-shadow-message);
}

/* ============================================
   CHAT LAYOUT INTEGRATION
   ============================================ */

.messages-area {
  overflow-y: auto;
  overflow-x: hidden;
  scroll-behavior: smooth;
  outline: none;
  background: linear-gradient(
    to bottom,
    var(--chat-bg-gradient-start) 0%,
    var(--chat-bg-gradient-end) 100%
  );
}

.messages-area::-webkit-scrollbar {
  width: 8px;
}

.messages-area::-webkit-scrollbar-track {
  background: var(--chat-bg-tertiary);
}

.messages-area::-webkit-scrollbar-thumb {
  background: var(--chat-border-primary);
  border-radius: var(--chat-radius-sm);
}

.messages-area::-webkit-scrollbar-thumb:hover {
  background: var(--chat-subtle);
}

.messages-area .chat-container {
  min-height: 100%;
  padding: var(--chat-space-3xl);
  max-width: var(--chat-layout-content-max-width);
  margin: 0 auto;
  width: 100%;
}

.messages-area .message-row {
  margin: var(--chat-space-lg) 0;
}

.messages-area .message {
  box-shadow: var(--chat-shadow-md);
  border-radius: var(--chat-radius-2xl);
  padding: var(--chat-space-lg) var(--chat-space-xl);
  max-width: 85%;
  font-size: var(--chat-font-size-lg);
  line-height: var(--chat-line-height-relaxed);
}

.messages-area .message.assistant {
  background: var(--chat-surface-primary);
  border: 1px solid var(--chat-border-primary);
}

.messages-area .message.user {
  background: linear-gradient(
    135deg,
    var(--chat-brand-primary) 0%,
    var(--chat-brand-primary-light) 100%
  );
  box-shadow: var(--chat-shadow-message-user);
}

.messages-area .avatar {
  width: 40px;
  height: 40px;
  box-shadow: var(--chat-shadow-avatar-lg);
  border: 2px solid var(--chat-surface-primary);
}

/* ============================================
   DARK MODE
   ============================================ */

body.dark-mode .chat-container {
  background: var(--chat-dark-bg-primary);
}

body.dark-mode .readonly-banner {
  background: var(--chat-status-warning-darkest);
  border-color: var(--chat-status-warning-dark);
  color: var(--chat-status-warning-bg);
}

body.dark-mode .message.assistant {
  color: var(--chat-dark-text-light);
  background: var(--chat-dark-bg-tertiary);
  border-color: var(--chat-dark-border-primary);
}

body.dark-mode .message.user {
  color: var(--chat-bg-primary);
  background: var(--chat-brand-primary);
}

body.dark-mode .message.error {
  background: var(--chat-status-error-darkest);
  color: var(--chat-status-error-light);
  border-color: var(--chat-status-error-darker);
}

body.dark-mode .message th {
  background: var(--chat-dark-bg-primary);
  color: var(--chat-dark-text-primary);
}

body.dark-mode .message td {
  border-color: var(--chat-dark-border-primary);
  color: var(--chat-dark-text-primary);
}

body.dark-mode .message code {
  background: var(--chat-dark-surface-secondary);
  color: var(--chat-dark-text-primary);
}

body.dark-mode .message pre {
  background: var(--chat-dark-surface-secondary);
}

body.dark-mode .message blockquote {
  border-left-color: var(--chat-dark-border-primary);
  color: var(--chat-dark-text-secondary);
}

body.dark-mode .message a {
  color: var(--chat-brand-primary-light);
}

body.dark-mode .message a:hover {
  color: var(--chat-brand-primary-lighter);
}

body.dark-mode .message hr {
  border-top-color: var(--chat-dark-border-primary);
}

body.dark-mode .typing-indicator {
  background: var(--chat-dark-surface-secondary);
  color: var(--chat-dark-text-secondary);
  border-color: var(--chat-dark-border-primary);
}

body.dark-mode .messages-area {
  background: linear-gradient(
    to bottom,
    var(--chat-dark-bg-gradient-start) 0%,
    var(--chat-dark-bg-gradient-end) 100%
  );
}

body.dark-mode .messages-area .message.assistant {
  background: var(--chat-dark-surface-secondary);
  border-color: var(--chat-dark-border-primary);
  color: var(--chat-dark-text-primary);
}

body.dark-mode .messages-area::-webkit-scrollbar-track {
  background: var(--chat-dark-bg-primary);
}

body.dark-mode .messages-area::-webkit-scrollbar-thumb {
  background: var(--chat-dark-border-primary);
}

body.dark-mode .chat-page .message th {
  background: var(--chat-dark-bg-primary);
}

/* ============================================
   RESPONSIVE
   ============================================ */

@media (max-width: 768px) {
  .message {
    max-width: 85vw;
  }

  .chat-container {
    padding: var(--chat-space-md);
  }

  .messages-area .chat-container {
    padding: var(--chat-space-2xl);
  }
}

@media (max-width: 480px) {
  .messages-area .chat-container {
    padding: var(--chat-space-lg);
  }

  .messages-area .message {
    max-width: 95%;
  }
}

/* ============================================
   LEGACY STYLES (from main.css)
   TODO: Refactor to use component tokens
   ============================================ */

/* Chat area container */
main#chat {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  gap: 2.5rem;
  padding: 3.5rem 2rem 2rem 2rem;
  overflow-y: auto;
  min-height: 0;
  max-height: calc(92vh - var(--header-height) - var(--footer-height));
  box-sizing: border-box;
}

/* Status messages */
.error {
  align-self: flex-start;
  background-color: #fee2e2;
  color: #dc2626;
  border: 1px solid #fecaca;
}

.success {
  align-self: flex-start;
  background-color: #d1fae5;
  color: #065f46;
  border: 1px solid #a7f3d0;
}

.typing-indicator {
  font-style: italic;
  color: #6b7280;
  font-size: 0.95rem;
  animation: blink 1s infinite alternate;
}

/* Readonly banner */
.readonly-banner {
  background-color: #fff3cd;
  color: #856404;
  border: 1px solid #ffeeba;
  padding: 0.8rem 1.2rem;
  font-size: 1rem;
  font-weight: 500;
  text-align: center;
  border-radius: var(--radius-md);
  margin: 1rem 2rem 0;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.06);
}

body.dark-mode .readonly-banner {
  background: #2d2f39;
  color: var(--clr-text);
}

/* Message layout with avatars */
.message-row {
  display: flex;
  align-items: flex-start;
  gap: 0.95rem;
  margin-bottom: 1.3rem;
  width: 100%;
  padding-left: 0.3px;
  padding-right: 18px;
}

.message-row.user {
  flex-direction: row-reverse;
  justify-content: flex-end;
}

.message-row.assistant {
  flex-direction: row;
  justify-content: flex-start;
}

.avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background-size: cover;
  background-position: center;
  flex-shrink: 0;
  margin-top: 0;
  box-shadow: 0 2px 10px rgba(90, 90, 90, 0.09);
}

.message {
  display: block;
  max-width: 920px;
  min-width: 150px;
  padding: 0.55rem 2rem 0.5rem 1.3rem;
  border-radius: 1.3rem 1.4rem 1.2rem 1.4rem;
  line-height: 1.52;
  word-break: break-word;
  box-shadow: 0 2px 14px rgba(0, 0, 0, 0.07);
  font-size: 0.95rem;
  margin: 0;
}

.message > *:first-child,
.message p:first-child {
  margin-top: 0 !important;
}

.message > *:last-child,
.message p:last-child {
  margin-bottom: 0 !important;
}

.message.user {
  background: var(--clr-surface-alt);
  color: #ffffff;
  margin-left: auto;
  border: 1px solid var(--clr-accent);
  border-top-right-radius: 0.5rem;
  border-bottom-right-radius: 1.45rem;
  border-top-left-radius: 1.45rem;
  border-bottom-left-radius: 1.45rem;
}

.message.assistant {
  background: var(--clr-surface);
  color: #111827;
  margin-right: auto;
  border: 1px solid var(--clr-border);
  border-top-left-radius: 0.5rem;
  border-top-right-radius: 1.45rem;
  border-bottom-left-radius: 1.45rem;
  border-bottom-right-radius: 1.45rem;
}

/* Dark mode for messages */
body.dark-mode .message.user {
  background: #4b4587;
  color: #fff;
  border: 1px solid var(--clr-accent);
}

body.dark-mode .message.assistant {
  background: #232534;
  color: var(--clr-text);
  border: 1px solid var(--clr-border);
}

/* Avatar positioning */
.message-row .avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  flex-shrink: 0;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
}

.message-row.user .avatar {
  background-image: url("user.png");
}

.message-row {
  align-items: flex-start;
}

.avatar {
  margin-top: 2px;
}

/* Rail central layout (ChatGPT-style) */
:root {
  --chat-col: 860px;
  --page-pad: 32px;
  --sidebar-w: 270px;
  --sidebar-gap: 16px;
  --rail-left-gap: 12px;
  --bubble-offset: 4px;
}

main#chat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.6rem;
  padding: 2rem var(--page-pad);
}

main#chat > .message-row,
#qpoqqcc-progress {
  width: min(var(--chat-col), 100% - (var(--page-pad) * 2));
  margin-left: auto;
  margin-right: auto;
}

/* Sidebar open - rail aligned left */
.app-layout.sidebar-open main#chat {
  align-items: flex-start;
  padding-left: calc(var(--sidebar-w) + var(--rail-left-gap));
  padding-right: var(--page-pad);
}

.app-layout.sidebar-open main#chat > .message-row,
.app-layout.sidebar-open #qpoqqcc-progress {
  margin-left: 0;
  margin-right: auto;
  width: min(
    var(--chat-col),
    calc(100% - (var(--sidebar-w) + var(--rail-left-gap) + var(--page-pad)))
  );
}

/* Bubble offset for polish */
.message-row.assistant {
  transform: translateX(calc(-1 * var(--bubble-offset)));
}

.message-row.user {
  transform: translateX(var(--bubble-offset));
}

/* Compact bubbles */
.message {
  padding: 0.95rem 1.15rem;
  line-height: 1.55;
}

.message-row + .message-row {
  margin-top: 0.35rem;
}

/* QPO progress bar positioning */
#qpoqqcc-progress {
  position: sticky;
  top: calc(var(--header-height) + 8px);
}

/* QPO bar aligned with message rail */
.main-content #qpoqqcc-progress {
  width: min(var(--chat-col), 100% - (var(--page-pad) * 2)) !important;
  margin-left: auto !important;
  margin-right: auto !important;
}

.app-layout.sidebar-open .main-content #qpoqqcc-progress {
  margin-left: calc(var(--sidebar-w) + var(--rail-left-gap)) !important;
  margin-right: auto !important;
  width: min(
    var(--chat-col),
    calc(100% - (var(--sidebar-w) + var(--rail-left-gap) + var(--page-pad)))
  ) !important;
}

/* Responsive mobile */
@media (max-width: 600px) {
  .message {
    max-width: 97vw;
    font-size: 0.875rem;
    padding: 0.85rem 0.7rem;
  }

  .avatar {
    width: 32px;
    height: 32px;
  }
}
