:root {
  --bg: #0a0a0c;
  --surface: #141418;
  --surface-2: #1e1e24;
  --surface-3: #272730;
  --text: #f0f0f5;
  --text-dim: #9ca3af;
  --accent: #5b8def;
  --accent-2: #7c5cff;
  --danger: #ef5b5b;
  --success: #5bef8d;
  --warning: #f5c75b;
  --border: #2a2a32;
  --radius: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

#app {
  height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 10;
  gap: 16px;
}

.app-header h1 {
  margin: 0;
  font-size: 1.15rem;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  white-space: nowrap;
}

#auth-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 0.9rem;
  color: var(--text-dim);
  min-width: 0;
}

#auth-bar .user-email {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 220px;
}

#auth-bar button {
  margin: 0;
  padding: 6px 12px;
  font-size: 0.85rem;
  width: auto;
}

main {
  display: grid;
  grid-template-columns: 280px 1fr;
  overflow: hidden;
}

/* Full-width content (login/register) spans both grid columns. */
.content.full {
  grid-column: 1 / -1;
}

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

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

.sidebar-header h2 {
  margin: 0;
  font-size: 0.95rem;
  color: var(--text);
}

.sidebar-nav {
  display: flex;
  gap: 8px;
}

.sidebar-nav button {
  margin: 0;
  padding: 6px 10px;
  font-size: 0.8rem;
  width: auto;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
}

.sidebar-nav button.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.session-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.session-item {
  padding: 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  position: relative;
}

.session-item:hover {
  border-color: var(--accent);
}

.session-item.active {
  background: var(--surface-3);
  border-color: var(--accent);
}

.session-item.offline {
  opacity: 0.6;
}

.session-name {
  font-weight: 600;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 4px;
  padding-right: 16px;
}

.session-detail {
  font-size: 0.78rem;
  color: var(--text-dim);
  line-height: 1.4;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.status-dot {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.status-dot.connected {
  background: var(--success);
  box-shadow: 0 0 6px var(--success);
}

.status-dot.disconnected {
  background: var(--danger);
}

.content {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
}

.content-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.content-header h2 {
  margin: 0;
  font-size: 1.1rem;
}

.content-header .title-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}

.content-header .title-spacer {
  flex: 1;
}

/* Small ghost icon button (rename pencil) next to the session title. */
.icon-btn {
  width: auto;
  margin: 0;
  padding: 4px;
  background: transparent;
  border: none;
  border-radius: 6px;
  color: var(--text-dim);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  transition: color 0.15s, background 0.15s;
}

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

.icon-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* Inline rename field that swaps in for the session title. */
.rename-input {
  margin: 0;
  padding: 2px 8px;
  width: min(420px, 60%);
  font-size: 1.05rem;
  font-weight: 600;
  background: var(--surface-2);
}

/* Compact-context button sits at the right end of the title row. */
#compact-btn {
  width: auto;
  margin: 0;
  white-space: nowrap;
}

.content-header .meta {
  font-size: 0.82rem;
  color: var(--text-dim);
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.empty-state {
  display: flex;
  flex: 1;
  align-items: center;
  justify-content: center;
  min-height: 100%;
  color: var(--text-dim);
  font-style: italic;
  text-align: center;
  padding: 40px;
}

/* Cards inside an empty-state (login/register) are real UI, not placeholder
text: they should neither be italic nor centered. */
.empty-state .card {
  font-style: normal;
  text-align: left;
}

.message {
  max-width: 85%;
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 0.95rem;
  line-height: 1.5;
  word-break: break-word;
}

/* Plain-text bodies (user messages, fallbacks) keep literal newlines; the
   container itself must NOT be pre-wrap or template whitespace between
   elements would render as blank lines. */
.message-text {
  white-space: pre-wrap;
}

.message-header {
  font-size: 0.75rem;
  font-weight: 600;
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  opacity: 0.8;
}

.message.user {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
}

.message.user .message-header {
  color: rgba(255,255,255,0.85);
}

.message.assistant {
  align-self: flex-start;
  background: var(--surface-2);
  border: 1px solid var(--border);
}

.message.tool {
  align-self: flex-start;
  background: transparent;
  border: 1px dashed var(--border);
  font-family: var(--mono);
  font-size: 0.85rem;
  color: var(--text-dim);
  max-width: 95%;
  width: 100%;
}

.message.tool pre {
  white-space: pre-wrap;
  word-break: break-word;
  margin: 0;
}

.message.thinking {
  align-self: flex-start;
  background: transparent;
  border: 1px solid var(--border);
  font-style: italic;
  color: var(--text-dim);
}

.message code {
  font-family: var(--mono);
  background: rgba(0,0,0,0.2);
  padding: 2px 5px;
  border-radius: 4px;
  font-size: 0.9em;
}

.message pre {
  background: rgba(0,0,0,0.2);
  padding: 10px;
  border-radius: 6px;
  overflow-x: auto;
  font-family: var(--mono);
  font-size: 0.85em;
  line-height: 1.4;
  margin: 6px 0;
}

.message pre:first-of-type { margin-top: 0; }
.message pre:last-of-type { margin-bottom: 0; }

.prompt-area {
  padding: 16px 20px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

/* Chat-style composer: the textarea auto-grows inside a rounded container
   and the controls live in their own toolbar row, so alignment never
   depends on the input's height. */
.composer {
  display: flex;
  flex-direction: column;
  gap: 6px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
  transition: border-color 0.15s;
}

.composer:focus-within {
  border-color: var(--accent);
}

.composer textarea {
  background: transparent;
  border: none;
  border-radius: 0;
  padding: 2px 4px;
  margin: 0;
  width: 100%;
  min-height: 26px;
  max-height: 160px;
  resize: none;
  line-height: 1.45;
}

.composer textarea:focus {
  outline: none;
  border: none;
}

.composer-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.composer .delivery-select {
  width: auto;
  margin: 0;
  height: 30px;
  padding: 0 26px 0 10px;
  font-size: 0.8rem;
  color: var(--text-dim);
  background-color: transparent;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%239ca3af' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e");
  background-repeat: no-repeat;
  background-position: right 8px center;
  background-size: 12px;
}

.composer button[type="submit"] {
  width: auto;
  margin: 0;
  height: 32px;
  padding: 0 18px;
  font-size: 0.85rem;
}

/* Session status line: model, context usage, tokens, cache, cost. */
.status-line {
  padding: 6px 20px;
  font-family: var(--mono);
  font-size: 0.78rem;
  color: var(--text-dim);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  overflow-x: auto;
}

/* Per-message model/usage annotation in the message header. */
.message-meta {
  font-weight: 400;
  opacity: 0.7;
  margin-left: 8px;
  text-transform: none;
  letter-spacing: 0;
}

/* Markdown elements produced by renderMarkdown. */
.message h3, .message h4, .message h5, .message h6 {
  margin: 12px 0 6px;
  font-size: 1rem;
  line-height: 1.3;
}
.message h3 { font-size: 1.1rem; }
.message h3:first-child, .message h4:first-child { margin-top: 0; }

.message ul, .message ol {
  margin: 6px 0;
  padding-left: 22px;
}

.message li { margin: 2px 0; }

.message p { margin: 8px 0; }
.message p:first-child { margin-top: 0; }
.message p:last-child { margin-bottom: 0; }

.message blockquote {
  border-left: 3px solid var(--border);
  margin: 8px 0;
  padding: 2px 12px;
  color: var(--text-dim);
}

.message hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 12px 0;
}

.message a {
  color: var(--accent);
  text-decoration: none;
}
.message a:hover { text-decoration: underline; }

.settings-page {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
}

.settings-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;
  max-width: 700px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

.card h2 {
  margin: 0 0 16px 0;
  font-size: 1.05rem;
}

.card h3 {
  margin: 0 0 12px 0;
  font-size: 1rem;
}

input, textarea, button, select {
  font: inherit;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  background: var(--surface-2);
  color: var(--text);
  width: 100%;
  margin-bottom: 10px;
}

input:focus, textarea:focus, button:focus, select:focus {
  outline: none;
  border-color: var(--accent);
}

button {
  cursor: pointer;
  font-weight: 600;
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  transition: opacity 0.15s, transform 0.05s;
}

button:hover { opacity: 0.9; }
button:active { transform: translateY(1px); }
button:disabled { opacity: 0.5; cursor: not-allowed; }

button.secondary {
  background: var(--surface-2);
  border-color: var(--border);
  color: var(--text);
}

button.danger {
  background: var(--danger);
  border-color: var(--danger);
}

button.small {
  width: auto;
  padding: 6px 12px;
  font-size: 0.85rem;
  margin-bottom: 0;
}

.form-group {
  margin-bottom: 16px;
}

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

.form-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}

.form-actions button {
  width: auto;
}

/* Inline form row (e.g. API key creation). */
.form-row {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 10px;
}

.form-row input[type="text"] {
  flex: 1;
  min-width: 160px;
  margin-bottom: 0;
}

.form-row button {
  margin-bottom: 0;
  width: auto;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
  font-size: 0.85rem;
  color: var(--text-dim);
}

.checkbox-label input {
  width: auto;
  margin: 0;
}

/* Visually hidden but available to assistive technology. */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.history-loading {
  text-align: center;
  color: var(--text-dim);
  font-size: 0.8rem;
  padding: 6px;
  font-style: italic;
}

.key-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.key-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  gap: 12px;
}

.key-name {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 8px;
}

.key-tags {
  font-size: 0.75rem;
  color: var(--text-dim);
}

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.badge.readonly { background: var(--warning); color: #000; }
.badge.revoked { background: var(--danger); color: #fff; }

.error { color: var(--danger); font-size: 0.9rem; }
.success { color: var(--success); font-size: 0.9rem; }

.setup-command {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  font-family: var(--mono);
  font-size: 0.85rem;
  word-break: break-all;
  margin: 12px 0;
  position: relative;
}

.setup-command .copy-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  width: auto;
  padding: 4px 10px;
  font-size: 0.75rem;
  margin: 0;
}

.toast {
  position: fixed;
  bottom: 20px;
  right: 20px;
  padding: 12px 18px;
  border-radius: 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  z-index: 100;
  animation: slideIn 0.2s ease-out;
}

.toast.success { border-color: var(--success); color: var(--success); }
.toast.error { border-color: var(--danger); color: var(--danger); }

@keyframes slideIn {
  from { transform: translateY(20px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

@media (max-width: 768px) {
  main {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }

  .content.full {
    grid-column: 1;
  }

  .sidebar {
    border-right: none;
    border-bottom: 1px solid var(--border);
    max-height: 35vh;
  }

  .app-header h1 { font-size: 1rem; }
  #auth-bar .user-email { max-width: 120px; }
  .message { max-width: 92%; }
}
