/* ═══════════════════════════════════════════════════
   Heretic — Dark luxury chat interface
   Typography: Syne (display) + DM Sans (body) + JetBrains Mono (code)
   ═══════════════════════════════════════════════════ */

:root {
  --bg:  #08080a;
  --bg2: #0e0e12;
  --bg3: #151519;
  --bg4: #1d1d23;
  --bg5: #27272e;
  --fg:  #e6e1db;
  --fg2: #8a857e;
  --fg3: #55524c;
  --accent: #c83c3c;
  --accent-hover: #da4a4a;
  --accent-subtle: rgba(200, 60, 60, 0.06);
  --accent-glow: rgba(200, 60, 60, 0.15);
  --green: #3dba8a;
  --yellow: #cfa240;
  --radius: 14px;
  --radius-sm: 8px;
  --sidebar-w: 280px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }

body {
  font-family: 'DM Sans', sans-serif;
  background: var(--bg);
  color: var(--fg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ── Scrollbars ──────────────────────────────────── */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--bg5); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--fg3); }

/* ── Selection ───────────────────────────────────── */
::selection { background: rgba(200, 60, 60, 0.25); color: var(--fg); }

/* ═══════════════════════════════════════════════════
   LOGIN SCREEN
   ═══════════════════════════════════════════════════ */

#login-screen {
  position: fixed; inset: 0; z-index: 200;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px; padding: 24px;
  background: var(--bg);
  background-image:
    radial-gradient(ellipse 500px 350px at 50% 38%, rgba(200, 60, 60, 0.05), transparent);
}
#login-screen.hidden { display: none; }

.login-brand { text-align: center; margin-bottom: 8px; }

.login-brand h2 {
  font-family: 'Syne', sans-serif;
  font-size: 36px; font-weight: 800;
  letter-spacing: -1.5px;
  color: var(--fg);
  line-height: 1;
}

.login-tagline {
  font-size: 11px; color: var(--fg3);
  letter-spacing: 3px; text-transform: uppercase;
  margin-top: 6px;
}

#login-screen input {
  padding: 12px 16px; width: 280px;
  border: 1px solid var(--bg4); border-radius: var(--radius-sm);
  background: var(--bg2); color: var(--fg);
  font-family: inherit; font-size: 14px;
  outline: none; transition: border-color 200ms, box-shadow 200ms;
}
#login-screen input:focus {
  border-color: var(--fg3);
  box-shadow: 0 0 0 3px var(--accent-subtle);
}
#login-screen input::placeholder { color: var(--fg3); }

#login-btns { display: flex; gap: 10px; margin-top: 4px; }
#login-error { color: var(--accent); font-size: 13px; min-height: 18px; }

/* ═══════════════════════════════════════════════════
   LAYOUT & HEADER
   ═══════════════════════════════════════════════════ */

#app { display: flex; flex-direction: column; height: 100dvh; }
#app.hidden { display: none; }

#header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 11px 18px;
  background: var(--bg2);
  border-bottom: 1px solid var(--bg4);
  flex-shrink: 0; z-index: 10;
}
#header-left { display: flex; align-items: center; gap: 12px; }
#header h1 {
  font-family: 'Syne', sans-serif;
  font-size: 15px; font-weight: 700; letter-spacing: -0.3px;
}
#header-right { display: flex; align-items: center; gap: 12px; }

#timer {
  font-size: 11px; color: var(--fg2);
  font-variant-numeric: tabular-nums;
  font-family: 'JetBrains Mono', monospace;
}
#cost-badge {
  font-size: 11px; font-weight: 500;
  color: var(--green);
  font-variant-numeric: tabular-nums;
  font-family: 'JetBrains Mono', monospace;
}

#sidebar-toggle {
  background: none; border: none; color: var(--fg2); font-size: 18px;
  cursor: pointer; padding: 4px 6px; border-radius: 6px;
  transition: all 200ms;
}
#sidebar-toggle:hover { background: var(--bg4); color: var(--fg); }

/* ═══════════════════════════════════════════════════
   BUTTONS
   ═══════════════════════════════════════════════════ */

.btn {
  padding: 10px 24px; border: none; border-radius: 24px;
  font-family: inherit; font-size: 14px; font-weight: 500;
  cursor: pointer; transition: all 200ms;
}
.btn:active { transform: scale(0.97); }

.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover {
  background: var(--accent-hover);
  box-shadow: 0 0 24px var(--accent-glow);
}

.btn-secondary { background: var(--bg4); color: var(--fg2); }
.btn-secondary:hover { background: var(--bg5); color: var(--fg); }

.btn-stop {
  background: var(--bg4); color: var(--fg2);
  font-family: inherit; font-size: 12px; padding: 6px 14px;
  border-radius: 20px; border: none; cursor: pointer;
  transition: all 200ms;
}
.btn-stop:hover { color: var(--accent); background: var(--bg5); }

/* ═══════════════════════════════════════════════════
   SIDEBAR
   ═══════════════════════════════════════════════════ */

#main { display: flex; flex: 1; overflow: hidden; }

#sidebar {
  width: var(--sidebar-w); min-width: var(--sidebar-w);
  background: var(--bg2);
  border-right: 1px solid var(--bg4);
  display: flex; flex-direction: column; flex-shrink: 0;
  transition: margin-left 250ms cubic-bezier(0.4, 0, 0.2, 1);
}
#sidebar.collapsed { margin-left: calc(-1 * var(--sidebar-w)); }

#sidebar-header { padding: 14px; border-bottom: 1px solid var(--bg4); }
#new-chat-btn {
  width: 100%; padding: 10px 14px;
  border: 1px dashed var(--bg5); border-radius: var(--radius-sm);
  background: transparent; color: var(--fg2);
  font-family: inherit; font-size: 13px; font-weight: 500;
  cursor: pointer; text-align: left;
  transition: all 200ms;
}
#new-chat-btn:hover { border-color: var(--fg3); color: var(--fg); border-style: solid; }

#conv-list { flex: 1; overflow-y: auto; padding: 6px 8px; }

.conv-item {
  display: flex; align-items: center;
  padding: 10px 12px; border-radius: var(--radius-sm);
  cursor: pointer; font-size: 13px; color: var(--fg2);
  gap: 8px; margin-bottom: 2px;
  border-left: 3px solid transparent;
  transition: all 150ms;
}
.conv-item:hover { background: var(--bg4); color: var(--fg); }
.conv-item.active {
  background: var(--accent-subtle);
  color: var(--fg);
  border-left-color: var(--accent);
}

.conv-title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-delete {
  display: none; background: none; border: none; color: var(--fg3);
  cursor: pointer; font-size: 15px; padding: 0 4px; flex-shrink: 0;
  transition: color 150ms;
}
.conv-item:hover .conv-delete { display: block; }
.conv-delete:hover { color: var(--accent); }

#sidebar-footer {
  padding: 14px; border-top: 1px solid var(--bg4);
  display: flex; align-items: center; justify-content: space-between;
  font-size: 12px;
}
#sidebar-footer span { color: var(--fg2); font-weight: 500; }
#logout-btn {
  background: none; border: none; color: var(--fg3);
  cursor: pointer; font-family: inherit; font-size: 12px;
  transition: color 200ms;
}
#logout-btn:hover { color: var(--accent); }

/* ═══════════════════════════════════════════════════
   CONTENT & BOOT SCREEN
   ═══════════════════════════════════════════════════ */

#content { flex: 1; display: flex; flex-direction: column; overflow: hidden; min-width: 0; }

#boot {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 24px; text-align: center; gap: 20px;
}
#boot.hidden { display: none; }

.boot-icon {
  font-size: 42px; opacity: 0.12;
  animation: pulse 3s ease-in-out infinite;
}

#boot-status { font-size: 14px; color: var(--fg2); max-width: 340px; line-height: 1.6; }

#progress-wrap {
  width: 280px; height: 4px;
  background: var(--bg4); border-radius: 2px; overflow: hidden;
}
#progress-bar {
  height: 100%; width: 0%;
  background: linear-gradient(90deg, var(--accent), #e06060);
  border-radius: 2px;
  transition: width 0.4s ease;
  box-shadow: 0 0 12px var(--accent-glow);
}

#progress-text {
  font-size: 12px; color: var(--fg3);
  font-variant-numeric: tabular-nums;
  font-family: 'JetBrains Mono', monospace;
}
#boot-error { color: var(--accent); font-size: 13px; max-width: 340px; word-break: break-word; }

/* ═══════════════════════════════════════════════════
   CHAT AREA & MESSAGES
   ═══════════════════════════════════════════════════ */

#chat-wrap { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
#chat-wrap.hidden { display: none; }

#messages {
  flex: 1; overflow-y: auto; padding: 20px;
  display: flex; flex-direction: column; gap: 12px;
  scroll-behavior: smooth; -webkit-overflow-scrolling: touch;
}

.msg {
  max-width: 82%; padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 14px; line-height: 1.6; word-wrap: break-word;
  animation: fadeInUp 0.2s ease-out;
}

.msg-user {
  background: var(--bg4); align-self: flex-end;
  border-bottom-right-radius: 4px;
  white-space: pre-wrap;
}
.msg-assistant {
  background: var(--bg3); align-self: flex-start;
  border-bottom-left-radius: 4px;
  border-left: 2px solid var(--accent);
}
.msg-thinking {
  color: var(--fg3); font-style: italic;
  border-left-color: transparent;
}
.msg-speed {
  font-size: 11px; color: var(--fg3); margin-top: 8px;
  font-family: 'JetBrains Mono', monospace;
  padding-top: 6px; border-top: 1px solid var(--bg4);
}

/* ── Images in messages ──────────────────────────── */
.msg-images { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.msg-images img {
  max-width: 280px; max-height: 280px;
  border-radius: var(--radius-sm);
  transition: transform 200ms;
}
.msg-images img:hover { transform: scale(1.02); }

#image-preview {
  display: flex; gap: 8px; padding: 10px 14px; overflow-x: auto;
  background: var(--bg2); border-top: 1px solid var(--bg4);
}
#image-preview:empty { display: none; }
.preview-item { position: relative; flex-shrink: 0; }
.preview-item img { height: 60px; border-radius: 6px; }
.preview-remove {
  position: absolute; top: -6px; right: -6px; width: 18px; height: 18px;
  background: var(--accent); color: #fff; border: none; border-radius: 50%;
  font-size: 12px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  line-height: 1; transition: transform 150ms;
}
.preview-remove:hover { transform: scale(1.15); }

#attach-btn {
  width: 40px; height: 40px; border-radius: 50%; border: none;
  background: var(--bg4); color: var(--fg2); cursor: pointer;
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
  transition: all 200ms;
}
#attach-btn:hover { background: var(--bg5); color: var(--fg); }

/* ── Message action buttons ──────────────────────── */
.msg-actions { display: flex; gap: 6px; margin-top: 8px; }
.msg-action-btn {
  background: none; border: 1px solid var(--bg5); color: var(--fg3);
  font-family: inherit; font-size: 11px; font-weight: 500;
  padding: 3px 10px; border-radius: 12px; cursor: pointer;
  transition: all 150ms;
}
.msg-action-btn:hover { color: var(--fg); border-color: var(--fg3); }

/* ── Code block copy button ──────────────────────── */
.code-block-wrap { position: relative; }
.code-copy-btn {
  position: absolute; top: 8px; right: 8px;
  background: var(--bg5); border: none; color: var(--fg3);
  font-family: inherit; font-size: 11px; font-weight: 500;
  padding: 4px 10px; border-radius: 6px; cursor: pointer;
  opacity: 0; transition: all 150ms;
}
.code-block-wrap:hover .code-copy-btn { opacity: 1; }
.code-copy-btn:hover { color: var(--fg); background: var(--fg3); }

/* ═══════════════════════════════════════════════════
   SYSTEM PROMPT
   ═══════════════════════════════════════════════════ */

#system-prompt-bar {
  padding: 8px 14px; background: var(--bg2);
  border-bottom: 1px solid var(--bg4);
  flex-shrink: 0;
}
#system-prompt-toggle {
  background: none; border: none; color: var(--fg3);
  font-family: inherit; font-size: 12px; font-weight: 500;
  cursor: pointer; padding: 2px 0;
  transition: color 200ms;
}
#system-prompt-toggle::before {
  content: '\25B8'; margin-right: 6px;
  display: inline-block; font-size: 10px;
}
#system-prompt-toggle:hover { color: var(--fg2); }

#system-prompt-wrap { display: none; margin-top: 8px; }
#system-prompt-wrap.open { display: block; }

#system-prompt {
  width: 100%; padding: 10px 12px;
  border: 1px solid var(--bg4); border-radius: var(--radius-sm);
  background: var(--bg); color: var(--fg);
  font-family: inherit; font-size: 13px;
  outline: none; resize: vertical;
  min-height: 40px; max-height: 120px;
  transition: border-color 200ms;
}
#system-prompt:focus { border-color: var(--fg3); }
#system-prompt::placeholder { color: var(--fg3); }

/* ── GPU select dropdown ─────────────────────────── */
.gpu-select {
  padding: 8px 12px; border: 1px solid var(--bg5); border-radius: var(--radius-sm);
  background: var(--bg2); color: var(--fg); font-family: inherit; font-size: 13px;
  outline: none; cursor: pointer; transition: border-color 200ms;
  -webkit-appearance: none; appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg width='10' height='6' viewBox='0 0 10 6' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%238a857e' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 10px center;
  padding-right: 30px;
}
.gpu-select:hover { border-color: var(--fg3); }
.gpu-select option { background: var(--bg3); color: var(--fg); }

/* ── GPU offline banner ──────────────────────────── */
#gpu-banner {
  display: flex; align-items: center; justify-content: center; gap: 14px;
  padding: 10px 14px; background: var(--bg3);
  border-top: 1px solid var(--bg4); flex-shrink: 0;
  font-size: 13px; color: var(--fg2);
}
#gpu-banner.hidden { display: none; }

/* ── Stop generation button ──────────────────────── */
#stop-gen-btn {
  display: none; margin: 10px auto; padding: 7px 20px;
  border: 1px solid var(--bg5); border-radius: 24px;
  background: var(--bg2); color: var(--fg2);
  font-family: inherit; font-size: 12px; font-weight: 500;
  cursor: pointer; transition: all 200ms;
}
#stop-gen-btn:hover { border-color: var(--fg3); color: var(--fg); }

/* ═══════════════════════════════════════════════════
   MARKDOWN IN ASSISTANT MESSAGES
   ═══════════════════════════════════════════════════ */

.msg-assistant p { margin: 0 0 10px 0; }
.msg-assistant p:last-child { margin-bottom: 0; }
.msg-assistant ul, .msg-assistant ol { margin: 0 0 10px 20px; }
.msg-assistant li { margin-bottom: 4px; }

.msg-assistant h1, .msg-assistant h2, .msg-assistant h3 {
  font-family: 'Syne', sans-serif;
  margin: 14px 0 8px 0; font-weight: 600;
}
.msg-assistant h1 { font-size: 17px; }
.msg-assistant h2 { font-size: 15px; }
.msg-assistant h3 { font-size: 14px; }

.msg-assistant blockquote {
  border-left: 3px solid var(--accent); padding-left: 14px; margin: 10px 0;
  color: var(--fg2); font-style: italic;
}

.msg-assistant pre {
  background: #0a0a0d; padding: 14px 16px;
  border-radius: var(--radius-sm);
  overflow-x: auto; margin: 10px 0;
  border: 1px solid var(--bg4);
}
.msg-assistant code {
  font-family: 'JetBrains Mono', monospace; font-size: 13px;
}
.msg-assistant p code, .msg-assistant li code {
  background: #0a0a0d; padding: 2px 6px; border-radius: 4px;
  border: 1px solid var(--bg4); font-size: 12.5px;
}

.msg-assistant table { border-collapse: collapse; margin: 10px 0; font-size: 13px; width: 100%; }
.msg-assistant th, .msg-assistant td { border: 1px solid var(--bg4); padding: 8px 12px; text-align: left; }
.msg-assistant th { background: var(--bg4); font-weight: 600; }

.msg-assistant a { color: var(--accent); text-decoration: none; }
.msg-assistant a:hover { text-decoration: underline; }
.msg-assistant hr { border: none; border-top: 1px solid var(--bg4); margin: 14px 0; }
.msg-assistant strong { font-weight: 600; }

/* ═══════════════════════════════════════════════════
   INPUT BAR
   ═══════════════════════════════════════════════════ */

#input-bar {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 12px 14px; background: var(--bg2);
  border-top: 1px solid var(--bg4);
  flex-shrink: 0;
}

#input {
  flex: 1; padding: 11px 16px;
  border: 1px solid var(--bg4); border-radius: 24px;
  background: var(--bg); color: var(--fg);
  font-family: inherit; font-size: 14px; line-height: 1.45;
  outline: none; max-height: 120px; resize: none;
  transition: border-color 200ms, box-shadow 200ms;
}
#input:focus {
  border-color: var(--fg3);
  box-shadow: 0 0 0 3px var(--accent-subtle);
}
#input::placeholder { color: var(--fg3); }

#send-btn {
  width: 42px; height: 42px; border-radius: 50%; border: none;
  background: var(--accent); color: #fff; cursor: pointer;
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
  transition: all 200ms;
}
#send-btn:hover:not(:disabled) {
  background: var(--accent-hover);
  box-shadow: 0 0 20px var(--accent-glow);
}
#send-btn:disabled { opacity: 0.2; cursor: default; }
#send-btn svg { width: 18px; height: 18px; fill: #fff; }

/* ═══════════════════════════════════════════════════
   MOBILE
   ═══════════════════════════════════════════════════ */

#sidebar-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.6); z-index: 50;
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
}

@media (max-width: 768px) {
  #sidebar {
    position: fixed; left: 0; top: 0; bottom: 0; z-index: 60;
    transition: transform 250ms cubic-bezier(0.4, 0, 0.2, 1);
  }
  #sidebar.collapsed { margin-left: 0; transform: translateX(-100%); }
  #sidebar:not(.collapsed) + #sidebar-overlay { display: block; }
  .msg { max-width: 92%; }
  #messages { padding: 14px; }
}

/* ═══════════════════════════════════════════════════
   ANIMATIONS
   ═══════════════════════════════════════════════════ */

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

@keyframes pulse {
  0%, 100% { opacity: 0.12; transform: scale(1); }
  50%      { opacity: 0.22; transform: scale(1.05); }
}
