/*
 * GenAI Chat frontend styles.
 *
 * Task 4.1 adds styling for the login view, the login error, and the chat-view
 * shell header (title + sign-out). The chat conversation/composer styling is
 * added in task 4.2, the model selector / metrics surface in task 12.1, and the
 * purple CMI badge in task 17.1.
 */

:root {
  --color-bg: #0f1419;
  --color-surface: #1a2029;
  --color-surface-alt: #232b36;
  --color-border: #313a47;
  --color-text: #e6e9ee;
  --color-text-muted: #9aa6b2;
  --color-accent: #4c8bf5;
  --color-accent-hover: #3f78d6;
  --color-error: #ff6b6b;
  /* Purple CMI (Custom Imported) badge palette (task 17.1, Req 17.2/17.3). */
  --color-cmi: #8b5cf6;
  --color-cmi-border: #7c3aed;
  --radius: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
    Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
}

body {
  font-family: var(--font);
  background: var(--color-bg);
  color: var(--color-text);
  line-height: 1.5;
}

#app,
.view {
  height: 100%;
}

/* Toggled via the [hidden] attribute from app.js view gating. */
[hidden] {
  display: none !important;
}

/* --------------------------------------------------------------------------
   Login view
   -------------------------------------------------------------------------- */
#login-view {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 32px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
}

.login-title {
  margin: 0 0 4px;
  font-size: 1.5rem;
}

.login-subtitle {
  margin: 0 0 24px;
  color: var(--color-text-muted);
  font-size: 0.95rem;
}

.field {
  display: block;
  margin-bottom: 16px;
}

.field-label {
  display: block;
  margin-bottom: 6px;
  font-size: 0.85rem;
  color: var(--color-text-muted);
}

.field input {
  width: 100%;
  padding: 10px 12px;
  font-size: 1rem;
  color: var(--color-text);
  background: var(--color-surface-alt);
  border: 1px solid var(--color-border);
  border-radius: 8px;
}

.field input:focus {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: 0 0 0 3px rgba(76, 139, 245, 0.25);
}

.login-error {
  margin: 0 0 16px;
  padding: 10px 12px;
  font-size: 0.9rem;
  color: var(--color-error);
  background: rgba(255, 107, 107, 0.12);
  border: 1px solid rgba(255, 107, 107, 0.35);
  border-radius: 8px;
}

/* --------------------------------------------------------------------------
   Buttons
   -------------------------------------------------------------------------- */
.btn {
  font-family: inherit;
  font-size: 0.95rem;
  padding: 10px 16px;
  border-radius: 8px;
  border: 1px solid transparent;
  cursor: pointer;
}

.btn:disabled {
  opacity: 0.6;
  cursor: default;
}

.btn-primary {
  width: 100%;
  color: #fff;
  background: var(--color-accent);
}

.btn-primary:hover:not(:disabled) {
  background: var(--color-accent-hover);
}

.btn-secondary {
  color: var(--color-text);
  background: transparent;
  border-color: var(--color-border);
}

.btn-secondary:hover {
  background: var(--color-surface-alt);
}

/* --------------------------------------------------------------------------
   Chat view shell (header + sign-out). Conversation/composer styling: task 4.2.
   -------------------------------------------------------------------------- */
#chat-view {
  display: flex;
  flex-direction: column;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  border-bottom: 1px solid var(--color-border);
  background: var(--color-surface);
}

.chat-title {
  margin: 0;
  font-size: 1.15rem;
}

.chat-header-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

/* --------------------------------------------------------------------------
   Model selector (task 12.1) — header dropdown populated from GET /api/models
   -------------------------------------------------------------------------- */
.model-select-label {
  font-size: 0.8rem;
  color: var(--color-text-muted);
}

.model-select {
  font-family: inherit;
  font-size: 0.9rem;
  max-width: 240px;
  padding: 8px 10px;
  color: var(--color-text);
  background: var(--color-surface-alt);
  border: 1px solid var(--color-border);
  border-radius: 8px;
  cursor: pointer;
}

.model-select:focus {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: 0 0 0 3px rgba(76, 139, 245, 0.25);
}

.model-select:disabled {
  opacity: 0.6;
  cursor: default;
}

/* Active state for the metrics toggle when the panel is open. */
.btn-secondary[aria-pressed="true"] {
  color: #fff;
  background: var(--color-accent);
  border-color: var(--color-accent);
}

/* --------------------------------------------------------------------------
   Chat body: conversation column + metrics side panel (task 12.1)
   -------------------------------------------------------------------------- */
.chat-body {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: row;
}

#chat-root {
  flex: 1;
  min-height: 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

/* --------------------------------------------------------------------------
   Chat conversation + composer (task 4.2)
   -------------------------------------------------------------------------- */

/* Scrollable conversation log; grows to fill the space above the composer. */
.chat-conversation {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.chat-empty {
  margin: auto;
  color: var(--color-text-muted);
  font-size: 0.95rem;
  text-align: center;
}

/* A single role-labeled turn. */
.chat-turn {
  max-width: 720px;
  width: fit-content;
  padding: 10px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--color-border);
  background: var(--color-surface);
}

.chat-turn-role {
  margin-bottom: 4px;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--color-text-muted);
}

/* Preserve the user's line breaks/whitespace and wrap long words safely. */
.chat-turn-content {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* User turns sit on the right with an accent treatment. */
.chat-turn--user {
  align-self: flex-end;
  background: var(--color-accent);
  border-color: var(--color-accent);
  color: #fff;
}

.chat-turn--user .chat-turn-role {
  color: rgba(255, 255, 255, 0.8);
}

/* Assistant turns sit on the left. */
.chat-turn--assistant {
  align-self: flex-start;
  background: var(--color-surface-alt);
}

/* Error turns are visually distinct but still part of the conversation. */
.chat-turn--error {
  align-self: flex-start;
  background: rgba(255, 107, 107, 0.12);
  border-color: rgba(255, 107, 107, 0.35);
  color: var(--color-error);
}

.chat-turn--error .chat-turn-role {
  color: var(--color-error);
}

/* Animated pending indicator shown while a request is in flight. */
.chat-pending-dots {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 1.2em;
}

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

.chat-pending-dots span:nth-child(1) {
  animation-delay: -0.24s;
}

.chat-pending-dots span:nth-child(2) {
  animation-delay: -0.12s;
}

@keyframes chat-pending-bounce {
  0%,
  80%,
  100% {
    transform: scale(0.5);
    opacity: 0.4;
  }
  40% {
    transform: scale(1);
    opacity: 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  .chat-pending-dots span {
    animation: none;
    opacity: 0.7;
  }
}

/* Composer: multi-line input + send control pinned below the conversation. */
.chat-composer {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  padding: 16px 20px;
  border-top: 1px solid var(--color-border);
  background: var(--color-surface);
}

.chat-input {
  flex: 1;
  resize: none;
  max-height: 200px;
  min-height: 44px;
  padding: 11px 12px;
  font-family: inherit;
  font-size: 1rem;
  line-height: 1.4;
  color: var(--color-text);
  background: var(--color-surface-alt);
  border: 1px solid var(--color-border);
  border-radius: 8px;
}

.chat-input:focus {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: 0 0 0 3px rgba(76, 139, 245, 0.25);
}

.chat-send {
  width: auto;
  flex: 0 0 auto;
}

/* --------------------------------------------------------------------------
   Inline metrics badges beneath assistant messages (task 12.1, Req 14.3/14.6)

   Badges live inside the assistant turn so they remain visible regardless of
   whether the metrics side panel is open.
   -------------------------------------------------------------------------- */
.chat-metrics-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.chat-metric-badge {
  display: inline-flex;
  align-items: baseline;
  gap: 4px;
  padding: 2px 8px;
  font-size: 0.72rem;
  line-height: 1.6;
  color: var(--color-text-muted);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 999px;
  white-space: nowrap;
}

.chat-metric-badge-label {
  text-transform: uppercase;
  letter-spacing: 0.03em;
  font-size: 0.64rem;
}

.chat-metric-badge-value {
  color: var(--color-text);
  font-weight: 600;
}

/* The model-name badge gets a subtle accent treatment. */
.chat-metric-badge--model {
  color: var(--color-text);
  border-color: var(--color-accent);
}

/* --------------------------------------------------------------------------
   CMI badge (task 17.1, Requirements 17.2 / 17.3)

   A purple pill rendered beside the role label on custom-imported (CMI)
   assistant responses and beside the model name on their metrics-panel
   entries. Native responses omit it entirely (Requirement 17.4). The label
   row and metrics-entry model line set it inline next to their text.
   -------------------------------------------------------------------------- */
.cmi-badge {
  display: inline-flex;
  align-items: center;
  margin-left: 6px;
  padding: 1px 7px;
  font-size: 0.62rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  line-height: 1.6;
  text-transform: uppercase;
  color: #fff;
  background: var(--color-cmi);
  border: 1px solid var(--color-cmi-border);
  border-radius: 999px;
  vertical-align: middle;
}

/* --------------------------------------------------------------------------
   Metrics side panel (task 12.1, Req 14.4 / 14.5)
   -------------------------------------------------------------------------- */
.metrics-panel {
  flex: 0 0 320px;
  max-width: 80vw;
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-left: 1px solid var(--color-border);
  background: var(--color-surface);
}

.metrics-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--color-border);
}

.metrics-panel-title {
  margin: 0;
  font-size: 1rem;
}

.metrics-close {
  font-size: 1.4rem;
  line-height: 1;
  color: var(--color-text-muted);
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 0 4px;
}

.metrics-close:hover {
  color: var(--color-text);
}

.metrics-panel-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.metrics-panel-empty {
  margin: 0;
  color: var(--color-text-muted);
  font-size: 0.9rem;
}

.metrics-entry {
  padding: 10px 12px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: var(--color-surface-alt);
}

.metrics-entry-model {
  margin-bottom: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--color-text);
}

.metrics-entry-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  font-size: 0.78rem;
}

.metrics-entry-row + .metrics-entry-row {
  margin-top: 2px;
}

.metrics-entry-key {
  color: var(--color-text-muted);
}

.metrics-entry-value {
  color: var(--color-text);
  font-variant-numeric: tabular-nums;
}

/* Stack the metrics panel below the conversation on narrow viewports. */
@media (max-width: 640px) {
  .chat-body {
    flex-direction: column;
  }

  .metrics-panel {
    flex: 0 0 auto;
    max-height: 45%;
    max-width: 100%;
    border-left: none;
    border-top: 1px solid var(--color-border);
  }
}
