/* ── Reset & base ─────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:        #0f1117;
  --surface:   #1a1d27;
  --surface2:  #22263a;
  --border:    #2e3350;
  --accent:    #5b6af0;
  --accent-h:  #7b8aff;
  --danger:    #e05b5b;
  --success:   #4caf83;
  --text:      #e2e4f0;
  --text-dim:  #7b80a0;
  --text-code: #a8d8a8;
  --radius:    10px;
  --font:      'Inter', system-ui, sans-serif;
  --mono:      'JetBrains Mono', 'Fira Code', monospace;
}

html, body { height: 100%; background: var(--bg); color: var(--text); font-family: var(--font); font-size: 14px; }

/* ── Layout ───────────────────────────────────────────────────────────── */
#app { display: flex; height: 100vh; overflow: hidden; }

nav {
  width: 220px; flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  padding: 20px 0;
}

nav .logo {
  padding: 0 20px 24px;
  font-size: 15px; font-weight: 700;
  color: var(--accent-h);
  letter-spacing: 0.5px;
}
nav .logo span { color: var(--text-dim); font-weight: 400; }

nav a {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 20px;
  color: var(--text-dim);
  text-decoration: none;
  font-size: 13px; font-weight: 500;
  border-left: 3px solid transparent;
  transition: color .15s, background .15s, border-color .15s;
  cursor: pointer;
}
nav a:hover { color: var(--text); background: var(--surface2); }
nav a.active { color: var(--text); border-left-color: var(--accent); background: var(--surface2); }
nav a svg { width: 16px; height: 16px; flex-shrink: 0; }

nav .spacer { flex: 1; }

#auth-status {
  padding: 12px 20px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-dim);
}
#auth-status strong { display: block; color: var(--text); margin-bottom: 2px; }
#logout-btn {
  margin-top: 8px;
  background: none; border: 1px solid var(--border);
  color: var(--text-dim); padding: 5px 10px;
  border-radius: 6px; cursor: pointer; font-size: 12px;
  transition: border-color .15s, color .15s;
  width: 100%;
}
#logout-btn:hover { border-color: var(--danger); color: var(--danger); }

main { flex: 1; overflow: hidden; padding: 32px; }
/* Conversations page needs full height — override main padding */
main:has(#page-conversations.active) { padding: 0; overflow: hidden; }

.page { display: none; max-width: 860px; margin: 0 auto; }
.page.active { display: block; }

h1 { font-size: 20px; font-weight: 700; margin-bottom: 4px; }
.subtitle { color: var(--text-dim); font-size: 13px; margin-bottom: 28px; }

/* ── Cards ────────────────────────────────────────────────────────────── */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 24px; margin-bottom: 20px;
}
.card-title {
  font-size: 14px; font-weight: 600;
  margin-bottom: 16px; color: var(--text);
}

/* ── Forms ────────────────────────────────────────────────────────────── */
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 12px; color: var(--text-dim); margin-bottom: 6px; font-weight: 500; }
.field input, .field select, .field textarea {
  width: 100%;
  background: var(--surface2); border: 1px solid var(--border);
  color: var(--text); padding: 9px 12px;
  border-radius: 7px; font-size: 13px; font-family: var(--font);
  outline: none; transition: border-color .15s;
}
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--accent); }
.field textarea { resize: vertical; min-height: 70px; }
.field select option { background: var(--surface2); }

.row { display: flex; gap: 12px; }
.row .field { flex: 1; }

/* ── Buttons ──────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 9px 18px; border-radius: 7px;
  font-size: 13px; font-weight: 600; cursor: pointer;
  border: none; transition: opacity .15s, background .15s;
}
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-h); }
.btn-danger  { background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled)  { opacity: .85; }
.btn-ghost   { background: var(--surface2); color: var(--text); border: 1px solid var(--border); }
.btn-ghost:hover:not(:disabled)   { border-color: var(--accent); color: var(--accent-h); }
.btn-sm { padding: 5px 12px; font-size: 12px; }

/* ── Alerts ───────────────────────────────────────────────────────────── */
.alert {
  padding: 10px 14px; border-radius: 7px;
  font-size: 13px; margin-top: 12px;
  display: none;
}
.alert.show { display: block; }
.alert-error   { background: rgba(224,91,91,.12);  color: #f08080; border: 1px solid rgba(224,91,91,.25); }
.alert-success { background: rgba(76,175,131,.12); color: #6dd9a8; border: 1px solid rgba(76,175,131,.25); }

/* ── Badge ────────────────────────────────────────────────────────────── */
.badge {
  display: inline-block; padding: 2px 8px; border-radius: 20px;
  font-size: 11px; font-weight: 600;
}
.badge-active  { background: rgba(76,175,131,.15); color: #6dd9a8; }
.badge-revoked { background: rgba(224,91,91,.15);  color: #f08080; }

/* ── Login screen ─────────────────────────────────────────────────────── */
#login-screen {
  display: flex; align-items: center; justify-content: center;
  height: 100vh; background: var(--bg);
}
.login-box { width: 380px; }
.login-logo { font-size: 22px; font-weight: 700; color: var(--accent-h); margin-bottom: 6px; }
.login-tagline { color: var(--text-dim); margin-bottom: 32px; font-size: 13px; }

.local-hint {
  display: none;
  font-size: 12px; color: var(--success);
  margin-bottom: 12px; padding: 8px 10px;
  background: rgba(76,175,131,.08);
  border-radius: 6px; border: 1px solid rgba(76,175,131,.2);
}

/* ── Conversation page ────────────────────────────────────────────────── */

/* The page itself fills the viewport — no card padding, no max-width cap */
#page-conversations {
  max-width: none !important;
  padding: 0 !important;
}
#page-conversations.active {
  height: 100vh;
  display: flex;
  flex-direction: column;
}
#page-conversations h1,
#page-conversations .subtitle { display: none; } /* title lives in the toolbar */

.conv-layout {
  display: flex;
  flex: 1;
  overflow: hidden;
  height: 100%;
}

/* ── Left sidebar ─────────────────────────────────────────────────────── */
.conv-sidebar {
  width: 240px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 16px 12px;
  gap: 10px;
}
.conv-sidebar-header {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: .6px;
  padding: 0 4px 4px;
}
.conv-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.conv-item {
  padding: 8px 10px;
  border-radius: 7px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-dim);
  transition: background .12s, color .12s;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border: none;
}
.conv-item:hover  { background: var(--surface2); color: var(--text); }
.conv-item.active { background: var(--surface2); color: var(--text); }
.conv-item.active::before {
  content: '';
  display: inline-block;
  width: 3px; height: 3px;
  background: var(--accent);
  border-radius: 50%;
  margin-right: 7px;
  flex-shrink: 0;
}
.conv-item .del-conv {
  color: var(--text-dim);
  font-size: 15px;
  line-height: 1;
  opacity: 0;
  padding: 0 2px;
  border-radius: 3px;
  flex-shrink: 0;
}
.conv-item:hover .del-conv { opacity: 1; }
.conv-item .del-conv:hover  { color: var(--danger); background: rgba(224,91,91,.12); }

/* ── Chat area ────────────────────────────────────────────────────────── */
.conv-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
  background: var(--bg);
}

/* Top toolbar */
.chat-toolbar {
  display: flex;
  align-items: center;
  padding: 12px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  gap: 10px;
  flex-shrink: 0;
}
.chat-toolbar-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}

/* Scrollable thread */
.chat-box {
  flex: 1;
  overflow-y: auto;
  padding: 32px 24px 16px;
  display: flex;
  flex-direction: column;
  gap: 0;
}
.chat-empty {
  color: var(--text-dim);
  font-size: 14px;
  text-align: center;
  margin: auto;
  line-height: 2;
}
.chat-empty strong { display: block; font-size: 18px; color: var(--text); margin-bottom: 6px; }

/* ── Message rows (ChatGPT-style) ─────────────────────────────────────── */
.msg {
  display: flex;
  flex-direction: column;
  width: 100%;
  padding: 20px 0;
  border-bottom: 1px solid rgba(255,255,255,.04);
}
.msg:last-child { border-bottom: none; }

/* User message — compact, right-aligned pill */
.msg.user {
  align-items: flex-end;
}
.msg.user .msg-bubble {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 18px 18px 4px 18px;
  max-width: 70%;
  font-size: 14px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}

/* Model response — full width prose */
.msg.model {
  align-items: flex-start;
}
.msg.model .msg-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  font-weight: 600;
  color: var(--accent-h);
  margin-bottom: 10px;
}
.msg.model .msg-label::before {
  content: '';
  display: inline-block;
  width: 20px; height: 20px;
  background: var(--accent);
  border-radius: 50%;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='white'%3E%3Cpath d='M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5'/%3E%3C/svg%3E");
  background-size: 12px;
  background-position: center;
  background-repeat: no-repeat;
}
.msg.model .msg-bubble {
  width: 100%;
  font-size: 15px;
  line-height: 1.75;
  color: var(--text);
}

/* User label — subtle, right-aligned */
.msg.user .msg-label {
  font-size: 11px;
  color: var(--text-dim);
  margin-bottom: 6px;
}

/* Streaming cursor */
.msg-bubble.streaming { opacity: .85; white-space: pre-wrap; }
.msg-bubble.streaming::after {
  content: '▋';
  animation: blink .6s infinite;
  margin-left: 2px;
  color: var(--accent);
}
@keyframes blink { 0%,100% { opacity: 1; } 50% { opacity: 0; } }

/* ── Markdown inside model bubbles ────────────────────────────────────── */
.msg-bubble.markdown { white-space: normal; }
.msg-bubble.markdown > *:first-child { margin-top: 0 !important; }
.msg-bubble.markdown > *:last-child  { margin-bottom: 0 !important; }

.msg-bubble.markdown p           { margin: 0 0 12px; }
.msg-bubble.markdown h1          { font-size: 20px; font-weight: 700; margin: 20px 0 8px; border-bottom: 1px solid var(--border); padding-bottom: 6px; }
.msg-bubble.markdown h2          { font-size: 17px; font-weight: 700; margin: 18px 0 7px; }
.msg-bubble.markdown h3          { font-size: 15px; font-weight: 600; margin: 14px 0 6px; }
.msg-bubble.markdown h4          { font-size: 14px; font-weight: 600; margin: 12px 0 5px; color: var(--text-dim); }
.msg-bubble.markdown ul,
.msg-bubble.markdown ol          { padding-left: 24px; margin: 4px 0 12px; }
.msg-bubble.markdown li          { margin-bottom: 5px; line-height: 1.65; }
.msg-bubble.markdown li p        { margin: 0; }
.msg-bubble.markdown li > ul,
.msg-bubble.markdown li > ol     { margin: 4px 0 4px; }
.msg-bubble.markdown strong      { font-weight: 700; color: var(--text); }
.msg-bubble.markdown em          { font-style: italic; color: var(--text-dim); }
.msg-bubble.markdown hr          { border: none; border-top: 1px solid var(--border); margin: 16px 0; }

/* Blockquote */
.msg-bubble.markdown blockquote {
  border-left: 3px solid var(--accent);
  padding: 8px 16px;
  margin: 10px 0;
  color: var(--text-dim);
  background: rgba(91,106,240,.06);
  border-radius: 0 8px 8px 0;
  font-style: italic;
}
.msg-bubble.markdown blockquote p { margin: 0; }

/* Inline code */
.msg-bubble.markdown code {
  font-family: var(--mono);
  font-size: 13px;
  background: rgba(0,0,0,.3);
  color: var(--text-code);
  padding: 2px 6px;
  border-radius: 4px;
  border: 1px solid rgba(255,255,255,.07);
}

/* Code blocks */
.code-block-wrap {
  position: relative;
  margin: 12px 0;
}
.code-block-wrap .code-lang {
  position: absolute;
  top: 0; left: 0;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-dim);
  padding: 7px 14px;
  text-transform: uppercase;
  letter-spacing: .4px;
}
.msg-bubble.markdown pre {
  background: #0c0e18;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 36px 16px 14px;
  overflow-x: auto;
  margin: 0;
}
.msg-bubble.markdown pre code {
  background: none;
  padding: 0;
  border: none;
  border-radius: 0;
  font-size: 13px;
  color: #c9d1d9;
  white-space: pre;
  line-height: 1.6;
}
.code-copy-btn {
  position: absolute;
  top: 7px; right: 10px;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 3px 10px;
  border-radius: 5px;
  font-size: 11px;
  cursor: pointer;
  opacity: 0;
  transition: opacity .15s, color .15s, border-color .15s;
}
.code-block-wrap:hover .code-copy-btn { opacity: 1; }
.code-copy-btn:hover { color: var(--accent-h); border-color: var(--accent); }

/* Tables */
.msg-bubble.markdown table {
  border-collapse: collapse;
  width: 100%;
  margin: 10px 0;
  font-size: 13px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
}
.msg-bubble.markdown th,
.msg-bubble.markdown td  { border: 1px solid var(--border); padding: 8px 14px; text-align: left; }
.msg-bubble.markdown th  { background: var(--surface2); font-weight: 600; font-size: 12px; color: var(--text-dim); }
.msg-bubble.markdown tr:nth-child(even) td { background: rgba(255,255,255,.02); }

/* ── Floating input bar ───────────────────────────────────────────────── */
.chat-input-wrap {
  padding: 16px 24px 20px;
  background: var(--bg);
  flex-shrink: 0;
}
.chat-input-box {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 10px 14px;
  transition: border-color .15s;
  max-width: 800px;
  margin: 0 auto;
}
.chat-input-box:focus-within { border-color: var(--accent); }
.chat-input-box textarea {
  flex: 1;
  background: none;
  border: none;
  color: var(--text);
  font-size: 14px;
  font-family: var(--font);
  outline: none;
  resize: none;
  min-height: 24px;
  max-height: 200px;
  line-height: 1.6;
  overflow-y: auto;
}
.chat-input-box textarea::placeholder { color: var(--text-dim); }
.chat-send-btn {
  flex-shrink: 0;
  width: 34px; height: 34px;
  background: var(--accent);
  border: none;
  border-radius: 9px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background .15s, opacity .15s;
}
.chat-send-btn:disabled { opacity: .35; cursor: not-allowed; }
.chat-send-btn:not(:disabled):hover { background: var(--accent-h); }
.chat-send-btn svg { width: 16px; height: 16px; fill: #fff; }
.chat-input-hint {
  text-align: center;
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 8px;
}

/* ── WS status pill ───────────────────────────────────────────────────── */
.ws-status {
  font-size: 11px; padding: 3px 9px; border-radius: 20px;
  display: inline-flex; align-items: center; gap: 5px;
  margin-left: auto;
  border: 1px solid transparent;
}
.ws-status::before { content: ''; width: 6px; height: 6px; border-radius: 50%; flex-shrink: 0; }
.ws-status.connected    { color: var(--success); border-color: rgba(76,175,131,.25); }
.ws-status.connected::before    { background: var(--success); }
.ws-status.disconnected { color: var(--text-dim); }
.ws-status.disconnected::before { background: var(--text-dim); }


/* ── Modals ───────────────────────────────────────────────────────────── */
.modal-backdrop {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,.6); z-index: 100;
  align-items: center; justify-content: center;
}
.modal-backdrop.open { display: flex; }
.modal {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 28px; width: 440px;
  box-shadow: 0 20px 60px rgba(0,0,0,.5);
}
.modal h2 { font-size: 16px; margin-bottom: 20px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }

/* ── API key table ────────────────────────────────────────────────────── */
.key-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.key-table th {
  text-align: left; padding: 8px 12px;
  color: var(--text-dim); font-weight: 500; font-size: 12px;
  border-bottom: 1px solid var(--border);
}
.key-table td { padding: 10px 12px; border-bottom: 1px solid var(--border); }
.key-table tr:last-child td { border-bottom: none; }
.key-table tr:hover td { background: var(--surface2); }

.copy-box {
  display: flex; align-items: center; gap: 8px;
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 7px; padding: 10px 14px;
  font-family: var(--mono); font-size: 12px;
  color: var(--text-code); word-break: break-all;
  margin-top: 12px;
}
.copy-box button {
  flex-shrink: 0; background: var(--surface); border: 1px solid var(--border);
  color: var(--text-dim); padding: 4px 10px; border-radius: 5px;
  cursor: pointer; font-size: 11px; transition: all .15s;
  margin-left: auto;
}
.copy-box button:hover { border-color: var(--accent); color: var(--accent-h); }

/* ── Misc ─────────────────────────────────────────────────────────────── */
.loading {
  display: inline-block; width: 14px; height: 14px;
  border: 2px solid transparent; border-top-color: currentColor;
  border-radius: 50%; animation: spin .6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.text-dim  { color: var(--text-dim); }
.monospace { font-family: var(--mono); font-size: 12px; color: var(--text-code); }
