:root {
  --bg: #0f1115;
  --panel: #161922;
  --panel-2: #1e2230;
  --border: #262b3a;
  --text: #e6e8ee;
  --muted: #8b93a7;
  --accent: #6ea8fe;
  --accent-strong: #4a8cff;
  --user-bg: #1e3a5f;
  --asst-bg: #1a1d28;
  --danger: #ef5b5b;
  --ok: #5bce8d;
  --radius: 10px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; height: 100%;
  background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}

button { font: inherit; cursor: pointer; }
button.primary {
  background: var(--accent-strong); color: white; border: 0;
  border-radius: 8px; padding: 8px 14px; font-weight: 600;
}
button.primary:hover { background: var(--accent); }
button.ghost {
  background: transparent; color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px;
}
button.ghost:hover { background: var(--panel-2); }
button.small { padding: 4px 8px; font-size: 13px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* ---------- Auth pages ---------- */
body.auth { display: grid; place-items: center; }
.auth-card {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 32px; min-width: 340px; max-width: 420px;
}
.auth-card h1 { margin: 0 0 4px; font-size: 24px; }
.auth-card label { display: block; margin: 16px 0 6px; font-size: 13px; color: var(--muted); }
.auth-card input[type="email"] {
  width: 100%; padding: 10px 12px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font: inherit;
}
.auth-card button[type="submit"] {
  margin-top: 16px; width: 100%; padding: 10px;
  background: var(--accent-strong); color: white; border: 0; border-radius: 8px; font-weight: 600;
}
.auth-card button[type="submit"]:hover { background: var(--accent); }
.banner { padding: 10px 12px; border-radius: 8px; margin: 12px 0; font-size: 14px; }
.banner.success { background: #143a28; color: var(--ok); border: 1px solid #1d5238; }
.banner.error { background: #3a1717; color: var(--danger); border: 1px solid #531d1d; }

/* ---------- App layout ---------- */
.app {
  display: grid; grid-template-columns: 260px 1fr; height: 100vh;
}
.sidebar {
  background: var(--panel); border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
}
.brand { padding: 16px; border-bottom: 1px solid var(--border); }
.brand h1 { margin: 0 0 10px; font-size: 18px; letter-spacing: 0.5px; }
.brand button { width: 100%; }
.conv-list { flex: 1; overflow-y: auto; padding: 8px; }
.conv-item {
  padding: 8px 10px; border-radius: 8px; cursor: pointer;
  display: flex; justify-content: space-between; align-items: center;
  font-size: 14px; color: var(--text);
}
.conv-item:hover { background: var(--panel-2); }
.conv-item.active { background: var(--panel-2); border: 1px solid var(--border); }
.conv-item .title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.conv-item .del { opacity: 0; transition: opacity 0.15s; color: var(--muted); padding: 0 6px; }
.conv-item:hover .del { opacity: 1; }
.conv-item .del:hover { color: var(--danger); }
.sidebar-foot {
  padding: 10px 16px; border-top: 1px solid var(--border);
  display: flex; align-items: center; gap: 8px;
}
.sidebar-foot span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- Chat ---------- */
.chat { display: flex; flex-direction: column; min-width: 0; }
.chat-head {
  padding: 14px 24px; border-bottom: 1px solid var(--border);
  display: flex; align-items: baseline; gap: 12px;
}
.chat-head h2 { margin: 0; font-size: 16px; font-weight: 600; }

.messages { flex: 1; overflow-y: auto; padding: 24px; }
.msg { max-width: 880px; margin: 0 auto 18px; }
.msg .role {
  font-size: 11px; color: var(--muted); text-transform: uppercase;
  letter-spacing: 1px; margin-bottom: 6px;
}
.msg .bubble {
  background: var(--asst-bg); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px 16px; line-height: 1.55;
}
.msg.user .bubble { background: var(--user-bg); border-color: #2a4d7a; }
.msg .bubble pre {
  background: #0a0c12; padding: 12px; border-radius: 8px; overflow-x: auto;
  font-family: var(--mono); font-size: 13px;
}
.msg .bubble code { font-family: var(--mono); font-size: 0.92em; }
.msg .bubble :not(pre) > code {
  background: var(--panel-2); padding: 2px 5px; border-radius: 4px;
}
.msg .bubble h1, .msg .bubble h2, .msg .bubble h3 { margin: 14px 0 8px; }
.msg .bubble p { margin: 8px 0; }
.msg .bubble ul, .msg .bubble ol { margin: 8px 0; padding-left: 22px; }
.msg .bubble blockquote {
  border-left: 3px solid var(--accent); padding-left: 12px; color: var(--muted); margin: 8px 0;
}
.msg .bubble table { border-collapse: collapse; margin: 10px 0; }
.msg .bubble th, .msg .bubble td { border: 1px solid var(--border); padding: 6px 10px; }
.msg .atts { margin-top: 6px; display: flex; flex-wrap: wrap; gap: 6px; }
.msg .att-chip {
  background: var(--panel-2); border: 1px solid var(--border);
  padding: 2px 8px; border-radius: 12px; font-size: 12px; color: var(--muted);
}

.composer {
  border-top: 1px solid var(--border); padding: 12px 24px 16px;
  background: var(--panel);
}
.quick-actions { display: flex; gap: 6px; margin-bottom: 8px; flex-wrap: wrap; }
.chip {
  background: var(--panel-2); border: 1px solid var(--border); color: var(--text);
  border-radius: 14px; padding: 4px 12px; font-size: 12px; cursor: pointer;
}
.chip:hover { background: #262b3a; border-color: #36405a; }
.chip.active {
  background: var(--accent-strong); border-color: var(--accent);
  color: white; font-weight: 600;
}
.chip.active:hover { background: var(--accent); }

.attachments-bar { display: flex; flex-wrap: wrap; gap: 6px; min-height: 0; margin-bottom: 6px; }
.attachments-bar:empty { display: none; }
.att-pill {
  background: var(--panel-2); border: 1px solid var(--border);
  padding: 4px 10px; border-radius: 16px; font-size: 12px;
  display: flex; align-items: center; gap: 6px;
}
.att-pill button {
  background: none; border: 0; color: var(--muted); cursor: pointer; padding: 0;
}
.att-pill.pending { opacity: 0.6; }

.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.icon-btn {
  background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; cursor: pointer; user-select: none;
}
.icon-btn:hover { background: #262b3a; }

#input {
  flex: 1; resize: none; max-height: 200px;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 12px; font: inherit; line-height: 1.4;
}
#input:focus { outline: none; border-color: var(--accent); }

.toggle {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--panel-2); border: 1px solid var(--border);
  padding: 6px 10px; border-radius: 8px; font-size: 13px; cursor: pointer;
}
.toggle input { cursor: pointer; }

/* ---------- Settings dialog ---------- */
dialog {
  background: var(--panel); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 24px; min-width: 420px;
}
dialog::backdrop { background: rgba(0,0,0,0.5); }
dialog h3 { margin: 0 0 16px; }
dialog label { display: block; margin: 12px 0 4px; font-size: 13px; color: var(--muted); }
dialog input {
  width: 100%; padding: 8px 10px; border-radius: 6px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text); font: inherit;
}
.settings-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }

.att-pill.image { padding: 2px 6px 2px 2px; }
.att-pill img.thumb {
  width: 28px; height: 28px; object-fit: cover; border-radius: 4px;
}
.att-pill .meta { font-size: 11px; color: var(--muted); }

/* Per-message action toolbar (Copy / Download) */
.msg .actions {
  display: flex; gap: 4px; margin-top: 6px;
  opacity: 0; transition: opacity 0.15s;
}
.msg:hover .actions, .msg .actions:focus-within { opacity: 1; }
.msg .actions button {
  background: var(--panel-2); color: var(--muted);
  border: 1px solid var(--border); border-radius: 6px;
  padding: 3px 8px; font-size: 12px; cursor: pointer;
  display: inline-flex; align-items: center; gap: 4px;
}
.msg .actions button:hover { background: #262b3a; color: var(--text); }
.msg .actions button.flash {
  background: #143a28; color: var(--ok); border-color: #1d5238;
}

/* Stats badge under assistant messages */
.msg .stats {
  font-size: 11px; color: var(--muted); margin-top: 6px;
  display: flex; gap: 12px; flex-wrap: wrap;
}
.msg .stats span { white-space: nowrap; }

/* Docs picker dialog */
#docs-dialog { min-width: 640px; max-width: 80vw; max-height: 80vh; }
.dialog-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.dialog-head h3 { margin: 0; }
.docs-list {
  display: grid; grid-template-columns: 1fr; gap: 6px;
  max-height: 60vh; overflow-y: auto; margin-top: 8px;
}
.doc-row {
  display: grid; grid-template-columns: 40px 1fr auto; gap: 12px;
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--panel-2); cursor: pointer; align-items: center;
}
.doc-row:hover { border-color: var(--accent); }
.doc-row.selected { border-color: var(--accent); background: #1d2942; }
.doc-row .icon {
  width: 40px; height: 40px; border-radius: 6px; background: var(--bg);
  display: grid; place-items: center; font-size: 18px; overflow: hidden;
}
.doc-row .icon img { width: 100%; height: 100%; object-fit: cover; }
.doc-row .meta { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.doc-row .meta .name {
  font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.doc-row .meta .stats {
  font-size: 11px; color: var(--muted); display: flex; gap: 10px; flex-wrap: wrap;
}
.doc-row .badge {
  font-size: 11px; padding: 2px 6px; border-radius: 10px;
  border: 1px solid var(--border); white-space: nowrap;
}
.doc-row .badge.ready { color: var(--ok); border-color: #1d5238; }
.doc-row .badge.parsing { color: var(--accent); border-color: #2a4d7a; }
.doc-row .badge.error { color: var(--danger); border-color: #531d1d; }
.doc-row .badge.pending { color: var(--muted); }

/* Flag-in-chip */
.chip .flag {
  display: inline-block; margin-right: 4px;
  font-size: 14px; line-height: 1;
}

/* Toggle on/off state */
.toggle:has(input:checked) {
  background: #1f3a2a; border-color: #2d5e42; color: var(--ok);
}

/* Make thumbnails feel clickable */
.att-pill img.thumb,
.doc-row .icon img {
  cursor: zoom-in;
}

/* Image preview lightbox */
.preview-dialog {
  border: 0; padding: 0; background: transparent;
  max-width: 90vw; max-height: 92vh;
}
.preview-dialog::backdrop { background: rgba(0,0,0,0.85); }
.preview-form { position: relative; display: grid; place-items: center; gap: 8px; }
.preview-form img {
  max-width: 90vw; max-height: 84vh;
  border-radius: 8px; box-shadow: 0 4px 30px rgba(0,0,0,0.5);
  background: #000;
}
.preview-close {
  position: absolute; top: 8px; right: 12px;
  background: rgba(0,0,0,0.55); color: white; border: 0;
  width: 36px; height: 36px; border-radius: 18px; font-size: 22px;
  cursor: pointer; display: grid; place-items: center;
}
.preview-close:hover { background: rgba(0,0,0,0.8); }

/* Cursor blink for streaming */
.cursor-blink::after {
  content: "▍"; display: inline-block; margin-left: 2px;
  animation: blink 1s steps(2) infinite; color: var(--accent);
}
@keyframes blink { 50% { opacity: 0; } }

/* Streamed Qwen3 reasoning panel — visible while the model thinks */
.thinking-panel {
  border: 1px dashed #36405a;
  border-radius: 8px;
  background: rgba(110,168,254,0.04);
  padding: 8px 12px;
  margin-bottom: 8px;
  font-size: 12.5px;
  color: var(--muted);
  font-style: italic;
  max-height: 240px;
  overflow-y: auto;
}
.thinking-panel .thinking-head {
  display: flex; align-items: center; gap: 6px;
  font-style: normal; font-weight: 600; color: var(--accent);
  margin-bottom: 4px; font-size: 11px;
  text-transform: uppercase; letter-spacing: 0.6px;
}
.thinking-panel .thinking-head .pulse {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--accent);
  animation: pulse 1.2s infinite ease-in-out;
}
@keyframes pulse { 0%,100%{opacity:0.3} 50%{opacity:1} }
.thinking-panel .thinking-body { white-space: pre-wrap; word-break: break-word; }

/* Pre-first-token "thinking" indicator */
.thinking-dots {
  display: inline-flex; align-items: center; gap: 6px;
  color: var(--muted); font-size: 13px;
}
.thinking-dots i {
  display: inline-block; width: 8px; height: 8px; border-radius: 50%;
  background: var(--accent);
  animation: tdots 1.2s infinite ease-in-out both;
}
.thinking-dots i:nth-child(2) { animation-delay: 0.18s; }
.thinking-dots i:nth-child(3) { animation-delay: 0.36s; }
@keyframes tdots {
  0%, 60%, 100% { transform: scale(0.4); opacity: 0.45; }
  30%           { transform: scale(1);   opacity: 1; }
}

/* ---------- Backdrop + hamburger (mobile only by default) ---------- */
.backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.55);
  z-index: 20; display: none;
}
.backdrop.show { display: block; }
#sidebar-toggle { display: none; font-size: 18px; padding: 4px 10px; }

/* ===========================================================
   Phone / narrow layout
   =========================================================== */
@media (max-width: 768px) {
  .app { grid-template-columns: 1fr; }

  /* Sidebar becomes a slide-out drawer */
  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0;
    width: 82vw; max-width: 320px;
    z-index: 30;
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    box-shadow: 4px 0 18px rgba(0,0,0,0.5);
  }
  .sidebar.open { transform: translateX(0); }

  #sidebar-toggle { display: inline-flex; }

  .chat-head {
    padding: 10px 14px;
    gap: 10px;
    align-items: center;
  }
  .chat-head h2 {
    font-size: 15px;
    overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
    flex: 1; min-width: 0;
  }
  #model-badge { display: none; } /* free up header real-estate */

  .messages { padding: 12px; }
  .msg { margin-bottom: 14px; }
  .msg .bubble { padding: 12px 14px; line-height: 1.5; }
  .msg .bubble pre { font-size: 12px; }
  .msg .actions { opacity: 1; } /* hover doesn't exist on touch */

  .composer {
    padding: 8px 12px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
  }
  .quick-actions { gap: 4px; }
  .chip { padding: 5px 10px; font-size: 12px; }

  .composer-row { gap: 6px; flex-wrap: wrap; }
  /* iOS will zoom on focus if input font is < 16px; bump up */
  #input { font-size: 16px; padding: 9px 11px; }
  .toggle { padding: 6px 9px; font-size: 12px; }
  .icon-btn { padding: 7px 10px; }
  #send { padding: 9px 14px; }

  /* Dialogs span the viewport */
  dialog { min-width: 0; width: 92vw; max-width: 92vw; padding: 16px; }
  #docs-dialog { width: 96vw; max-width: 96vw; max-height: 86vh; }
  .docs-list { max-height: 70vh; }
  .doc-row { grid-template-columns: 36px 1fr; }
  .doc-row .badge { grid-column: 2; justify-self: start; margin-top: 4px; }

  /* Preview lightbox: fill more on phone */
  .preview-form img { max-width: 96vw; max-height: 80vh; }

  /* Thinking panel slimmer */
  .thinking-panel { max-height: 180px; font-size: 12px; padding: 6px 10px; }
}

/* very small phones */
@media (max-width: 380px) {
  .quick-actions { gap: 3px; }
  .chip { padding: 4px 8px; font-size: 11.5px; }
  .toggle span { display: none; } /* hide label, keep checkbox */
}
