*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: #0f1117;
  color: #e8e8e8;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 20px;
  background: #1a1d27;
  border-bottom: 1px solid #2a2d3a;
  flex-shrink: 0;
}

header svg { color: #7c6af7; }

header h1 {
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

#token-container {
  flex-grow: 1;
  margin-left: 50px;
  display: flex;
  align-items: center;
  gap: 6px;
}

#assistant-id-input {
  background: #23263a;
  border: 1px solid #2e3148;
  border-radius: 6px;
  color: #e8e8e8;
  font-size: 0.75rem;
  padding: 5px 10px;
  width: 200px;
  outline: none;
  transition: border-color 0.15s;
  font-family: "JetBrains Mono", "Fira Code", Consolas, monospace;
}

#assistant-id-input:focus { border-color: #5b5bd6; }
#assistant-id-input::placeholder { color: #555; }

#bearer-token-input {
  background: #23263a;
  border: 1px solid #2e3148;
  border-radius: 6px;
  color: #e8e8e8;
  font-size: 0.75rem;
  padding: 5px 10px;
  width: 400px;
  outline: none;
  transition: border-color 0.15s;
  font-family: "JetBrains Mono", "Fira Code", Consolas, monospace;
}

#bearer-token-input:focus { border-color: #5b5bd6; }
#bearer-token-input::placeholder { color: #555; }
#bearer-token-input.has-token { border-color: #4ade80; }

#delete-token-btn {
  background: none;
  border: 1px solid #2e3148;
  border-radius: 6px;
  color: #888;
  cursor: pointer;
  padding: 5px 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}
#delete-token-btn:hover { color: #f87171; border-color: #f87171; background: #23263a; }
#delete-token-btn svg { pointer-events: none; }

#role-select {
  background: #23263a;
  border: 1px solid #2e3148;
  border-radius: 6px;
  color: #888;
  font-size: 0.75rem;
  padding: 5px 10px;
  outline: none;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}
#role-select:focus { border-color: #5b5bd6; }
#role-select.has-role { color: #e8e8e8; }

#conv-badge {
  font-size: 0.75rem;
  color: #888;
  background: #23263a;
  border: 1px solid #2e3148;
  border-radius: 6px;
  padding: 5px 10px;
  max-width: 280px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scroll-behavior: smooth;
}

.msg {
  display: flex;
  gap: 12px;
  max-width: 780px;
  width: 100%;
}

.msg.user { align-self: flex-end; flex-direction: row-reverse; }
.msg.assistant { align-self: flex-start; flex-wrap: wrap; }

/* Action row shown below the assistant bubble on hover */
.msg-actions {
  width: 100%;
  padding-left: 44px; /* avatar (32px) + gap (12px) */
  display: flex;
  justify-content: flex-start;
  gap: 6px;
  margin-top: -4px;
  opacity: 0;
  transition: opacity 0.15s;
  pointer-events: none;
}
.msg.assistant:hover .msg-actions { opacity: 1; pointer-events: auto; }

.copy-btn {
  background: #1a1d27;
  border: 1px solid #2e3148;
  border-radius: 6px;
  color: #666;
  cursor: pointer;
  padding: 3px 9px;
  font-size: 0.72rem;
  font-family: inherit;
  display: flex;
  align-items: center;
  gap: 5px;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}
.copy-btn:hover { color: #e8e8e8; border-color: #5b5bd6; background: #23263a; }
.copy-btn.copied { color: #4ade80; border-color: #4ade80; }
.copy-btn svg { pointer-events: none; flex-shrink: 0; }

/* ── Rating buttons ──────────────────────────────────────────────────── */
.rating-btns {
  display: flex;
  gap: 4px;
  margin-left: 4px;
}
.rating-btn {
  background: #1a1d27;
  border: 1px solid #2e3148;
  border-radius: 6px;
  color: #666;
  cursor: pointer;
  padding: 4px 7px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}
.rating-btn:hover { color: #e8e8e8; border-color: #5b5bd6; background: #23263a; }
.rating-btn:disabled { cursor: default; opacity: 0.4; }
.rating-btn:disabled:hover { color: #666; border-color: #2e3148; background: #1a1d27; }
.rating-btn.rated.rating-up { color: #4ade80; border-color: #4ade80; opacity: 1; }
.rating-btn.rated.rating-down { color: #f87171; border-color: #f87171; opacity: 1; }
.rating-btn svg { pointer-events: none; flex-shrink: 0; }

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.8rem;
  font-weight: 700;
}

.msg.user .avatar { background: #5b5bd6; color: #fff; }
.msg.assistant .avatar { background: #2a2d3a; color: #7c6af7; font-size: 1rem; }

.bubble {
  padding: 12px 16px;
  border-radius: 14px;
  line-height: 1.6;
  font-size: 0.92rem;
  word-break: break-word;
  max-width: calc(100% - 44px);
}

/* User input is plain text — preserve whitespace as typed */
.msg.user .bubble { white-space: pre-wrap; }

.msg.user .bubble {
  background: #5b5bd6;
  color: #fff;
  border-bottom-right-radius: 4px;
}

.msg.assistant .bubble {
  background: #1e2130;
  border: 1px solid #2a2d3a;
  border-bottom-left-radius: 4px;
}

/* ── Markdown elements inside assistant bubbles ───────────────────────── */
.msg.assistant .bubble > *:first-child { margin-top: 0 !important; }
.msg.assistant .bubble > *:last-child  { margin-bottom: 0 !important; }

.msg.assistant .bubble p { margin: 0 0 0.65em; }

.msg.assistant .bubble h1,
.msg.assistant .bubble h2,
.msg.assistant .bubble h3,
.msg.assistant .bubble h4 { margin: 0.85em 0 0.35em; font-weight: 600; line-height: 1.3; }
.msg.assistant .bubble h1 { font-size: 1.25em; }
.msg.assistant .bubble h2 { font-size: 1.1em; }
.msg.assistant .bubble h3 { font-size: 1em; }

.msg.assistant .bubble ul,
.msg.assistant .bubble ol { margin: 0 0 0.65em; padding-left: 1.6em; }
.msg.assistant .bubble li { margin: 0.2em 0; }
.msg.assistant .bubble li > p { margin-bottom: 0.3em; }

.msg.assistant .bubble code {
  background: #12141f;
  border: 1px solid #2a2d3a;
  border-radius: 4px;
  padding: 0.15em 0.45em;
  font-family: "JetBrains Mono", "Fira Code", Consolas, monospace;
  font-size: 0.84em;
  color: #c9d1e8;
}

.msg.assistant .bubble pre {
  background: #12141f;
  border: 1px solid #2a2d3a;
  border-radius: 8px;
  padding: 14px 16px;
  overflow-x: auto;
  margin: 0 0 0.65em;
}
.msg.assistant .bubble pre code {
  background: none; border: none; padding: 0;
  font-size: 0.85em; color: #c9d1e8;
}

.msg.assistant .bubble blockquote {
  border-left: 3px solid #5b5bd6;
  margin: 0 0 0.65em;
  padding: 6px 14px;
  color: #aaa;
  font-style: italic;
}

.msg.assistant .bubble hr {
  border: none; border-top: 1px solid #2a2d3a; margin: 0.8em 0;
}

.msg.assistant .bubble table {
  border-collapse: collapse; width: 100%; margin: 0 0 0.65em; font-size: 0.88em;
}
.msg.assistant .bubble th,
.msg.assistant .bubble td { border: 1px solid #2a2d3a; padding: 6px 10px; text-align: left; }
.msg.assistant .bubble th { background: #23263a; font-weight: 600; }

.msg.assistant .bubble strong { font-weight: 700; color: #f0f0f0; }

.msg.assistant .bubble a {
  color: #7c9cff; text-decoration: underline; text-underline-offset: 2px;
}
.msg.assistant .bubble a:hover { color: #a8bfff; }

.stream-status {
  font-size: 0.78rem;
  color: #7c6af7;
  opacity: 0.8;
  font-style: italic;
  margin-bottom: 6px;
  display: flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}

.stream-status::before {
  content: '';
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #7c6af7;
  animation: pulse 1.2s ease-in-out infinite;
  flex-shrink: 0;
}

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.3; transform: scale(0.7); }
}

.cursor {
  display: inline-block;
  width: 2px;
  height: 1em;
  background: #7c6af7;
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: blink 0.8s step-end infinite;
}

@keyframes blink { 50% { opacity: 0; } }

.error-bubble {
  color: #f87171;
  border-color: #4b1f1f;
  background: #1f1515;
}

#composer {
  padding: 16px 20px;
  border-top: 1px solid #2a2d3a;
  background: #1a1d27;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: flex-end;
  flex-shrink: 0;
}

#prompt-input {
  flex: 1;
  background: #23263a;
  border: 1px solid #2e3148;
  border-radius: 12px;
  color: #e8e8e8;
  font-size: 0.95rem;
  line-height: 1.5;
  padding: 10px 14px;
  resize: none;
  outline: none;
  min-height: 44px;
  max-height: 200px;
  overflow: hidden;
  transition: border-color 0.15s;
  font-family: inherit;
}

#prompt-input:focus { border-color: #5b5bd6; }
#prompt-input::placeholder { color: #555; }

#send-btn {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  border: none;
  background: #5b5bd6;
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, opacity 0.15s;
}

#send-btn:hover { background: #4f4fc4; }
#send-btn:disabled { opacity: 0.4; cursor: not-allowed; }
#send-btn.stop { background: #b3453f; }
#send-btn.stop:hover { background: #cc4f48; }

#send-btn svg { pointer-events: none; }

#reset-btn {
  background: none;
  border: 1px solid #2e3148;
  border-radius: 6px;
  color: #888;
  font-size: 0.75rem;
  padding: 5px 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 5px;
  transition: color 0.15s, border-color 0.15s;
  white-space: nowrap;
}
#reset-btn:hover { color: #e8e8e8; border-color: #5b5bd6; }
#reset-btn svg { pointer-events: none; }

/* scrollbar */
#messages::-webkit-scrollbar { width: 6px; }
#messages::-webkit-scrollbar-track { background: transparent; }
#messages::-webkit-scrollbar-thumb { background: #2a2d3a; border-radius: 3px; }

/* empty state */
#empty-state {
  margin: auto;
  text-align: center;
  color: #444;
  user-select: none;
}
#empty-state svg { opacity: 0.3; margin-bottom: 12px; }
#empty-state p { font-size: 0.9rem; }

/* ── Examples bar & button ─────────────────────────────────────────────── */
#examples-bar {
  flex-basis: 100%;
}

#examples-btn {
  background: none;
  border: 1px solid #2e3148;
  border-radius: 8px;
  color: #888;
  font-size: 0.75rem;
  padding: 4px 12px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  transition: color 0.15s, border-color 0.15s;
  font-family: inherit;
}
#examples-btn:hover { color: #e8e8e8; border-color: #5b5bd6; }

/* ── Examples modal ────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal-content {
  background: #1a1d27;
  border: 1px solid #2a2d3a;
  border-radius: 16px;
  max-width: 560px;
  width: 100%;
  max-height: 80vh;
  overflow-y: auto;
  padding: 24px;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
}

.modal-header h2 {
  font-size: 1rem;
  font-weight: 600;
  color: #e8e8e8;
}

.modal-close {
  background: none;
  border: 1px solid #2e3148;
  border-radius: 6px;
  color: #888;
  font-size: 0.72rem;
  padding: 4px 10px;
  cursor: pointer;
  font-family: inherit;
  transition: color 0.15s, border-color 0.15s;
}
.modal-close:hover { color: #e8e8e8; border-color: #5b5bd6; }

.examples-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

@media (max-width: 500px) {
  .examples-grid { grid-template-columns: 1fr; }
}

.example-item {
  background: #23263a;
  border: 1px solid #2e3148;
  border-radius: 10px;
  padding: 12px 14px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.example-item:hover { border-color: #5b5bd6; background: #282b42; }

.example-item .example-label {
  font-size: 0.8rem;
  font-weight: 600;
  color: #e8e8e8;
  display: flex;
  align-items: center;
  gap: 6px;
}

.example-item .example-prompt {
  font-size: 0.75rem;
  color: #888;
  line-height: 1.4;
}

/* ── Conversation history panel ──────────────────────────────────────────── */
#history-toggle {
  background: none;
  border: 1px solid #2e3148;
  border-radius: 6px;
  color: #888;
  cursor: pointer;
  padding: 5px 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}
#history-toggle:hover { color: #e8e8e8; border-color: #5b5bd6; background: #23263a; }
#history-toggle svg { pointer-events: none; color: currentColor; }

#history-panel {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 280px;
  background: #161922;
  border-right: 1px solid #2a2d3a;
  display: flex;
  flex-direction: column;
  transform: translateX(-100%);
  transition: transform 0.2s ease;
  z-index: 40;
}
#history-panel.open { transform: translateX(0); }

#history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 14px 14px 14px 18px;
  border-bottom: 1px solid #2a2d3a;
  font-size: 0.8rem;
  font-weight: 600;
  color: #e8e8e8;
  flex-shrink: 0;
}

#history-refresh {
  background: none;
  border: 1px solid #2e3148;
  border-radius: 6px;
  color: #888;
  cursor: pointer;
  padding: 4px 6px;
  display: flex;
  align-items: center;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}
#history-refresh:hover { color: #e8e8e8; border-color: #5b5bd6; background: #23263a; }
#history-refresh svg { pointer-events: none; color: currentColor; }
#history-refresh.spinning svg { animation: history-spin 0.8s linear infinite; }

@keyframes history-spin {
  to { transform: rotate(360deg); }
}

#history-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.history-state {
  padding: 16px 10px;
  font-size: 0.75rem;
  color: #666;
  line-height: 1.5;
  text-align: center;
}
.history-state.error { color: #f87171; }

.history-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
  border-radius: 6px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.history-item:hover { background: #1f2230; }
.history-item.active { background: #23263a; border-color: #5b5bd6; }
.history-item.pending { opacity: 0.6; pointer-events: none; }

.history-item-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.history-item-title {
  font-size: 0.78rem;
  color: #e8e8e8;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.history-item-title.untitled { color: #777; font-style: italic; }

.history-item-meta {
  font-size: 0.68rem;
  color: #666;
}

.history-item-actions {
  display: none;
  gap: 4px;
  flex-shrink: 0;
}
.history-item:hover .history-item-actions,
.history-item.active .history-item-actions { display: flex; }

.history-action-btn {
  background: none;
  border: none;
  border-radius: 4px;
  color: #777;
  cursor: pointer;
  padding: 3px;
  display: flex;
  align-items: center;
  transition: color 0.15s, background 0.15s;
}
.history-action-btn:hover { color: #e8e8e8; background: #2e3148; }
.history-action-btn.delete:hover { color: #f87171; }
.history-action-btn svg { pointer-events: none; }

.history-rename-input {
  flex: 1;
  min-width: 0;
  background: #0f1117;
  border: 1px solid #5b5bd6;
  border-radius: 4px;
  color: #e8e8e8;
  font-size: 0.78rem;
  font-family: inherit;
  padding: 4px 6px;
  outline: none;
}

#history-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 30;
  display: none;
}
#history-backdrop.visible { display: block; }

/* Cancelled (stopped) turns replayed from history */
.turn-note {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-top: 8px;
  font-size: 0.7rem;
  border-radius: 4px;
  padding: 2px 6px;
}

.stopped-note {
  color: #f0b429;
  border: 1px solid #6b5416;
  background: #2a2313;
}

.failed-note {
  color: #f87171;
  border: 1px solid #7f1d1d;
  background: #2a1414;
}

/* Wide viewports: panel pushes the layout instead of overlaying it */
@media (min-width: 1100px) {
  body.history-open { padding-left: 280px; }
  body.history-open #history-backdrop { display: none; }
}
