:root {
  --bg-0: #0e2540;
  --bg-1: #1b4d8c;
  --bg-2: #3fa9e0;
  --accent: #1b4d8c;
  --accent-2: #3fa9e0;
  --accent-glow: rgba(63, 169, 224, 0.4);
  --text: #f0f4ff;
  --text-dim: #b3c1de;
  --text-mute: #8698be;
  --panel: rgba(255, 255, 255, 0.07);
  --panel-border: rgba(63, 169, 224, 0.22);
  --user-bubble: linear-gradient(135deg, #0e2540 0%, #1b4d8c 50%, #3fa9e0 100%);
  --bot-bubble: rgba(255, 255, 255, 0.09);
  --success: #35d0a1;
  --error: #ff6b78;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: Inter, sans-serif;
  color: var(--text);
  background: linear-gradient(135deg, #0e2540 0%, #1b4d8c 50%, #3fa9e0 100%);
  overflow: hidden;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  background-image: linear-gradient(rgba(255, 255, 255, 0.06) 1px, transparent 1px),
                    linear-gradient(90deg, rgba(255, 255, 255, 0.06) 1px, transparent 1px);
  background-size: 60px 60px;
  opacity: 0.6;
}

.app { position: relative; z-index: 1; }

.app {
  display: grid;
  grid-template-columns: 320px 1fr;
  height: 100vh;
}

/* -------- Sidebar -------- */

.sidebar {
  padding: 22px 20px;
  border-right: 1px solid var(--panel-border);
  background: rgba(14, 37, 64, 0.55);
  backdrop-filter: blur(10px);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 18px;
  transition: transform 0.24s ease;
}

.brand { display: flex; align-items: center; gap: 12px; }

.brand-mark {
  width: 36px; height: 36px; border-radius: 10px;
  background: linear-gradient(135deg, #3fa9e0 0%, #ffffff 100%);
  box-shadow: 0 4px 20px var(--accent-glow);
  border: 1px solid rgba(255, 255, 255, 0.6);
  position: relative;
}
.brand-mark::after {
  content: ""; position: absolute; inset: 8px; border-radius: 4px;
  background: #0e2540;
}

.brand h1 { margin: 0; font-size: 15px; font-weight: 600; letter-spacing: 0.2px; }
.brand-sub { font-size: 11.5px; color: var(--text-dim); margin-top: 2px; }

.new-chat-btn {
  background: linear-gradient(135deg, rgba(77, 140, 255, 0.16), rgba(122, 183, 255, 0.08));
  border: 1px solid rgba(122, 183, 255, 0.3);
  color: var(--text);
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.15s ease;
}
.new-chat-btn:hover {
  background: linear-gradient(135deg, rgba(77, 140, 255, 0.26), rgba(122, 183, 255, 0.16));
  transform: translateY(-1px);
}

.panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 14px;
}
.panel h3 {
  margin: 0 0 10px;
  font-size: 11px;
  color: var(--text-mute);
  text-transform: uppercase;
  letter-spacing: 1.2px;
  font-weight: 600;
}

.status { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--text-mute); box-shadow: 0 0 8px transparent;
}
.dot.ok { background: var(--success); box-shadow: 0 0 8px var(--success); }
.dot.err { background: var(--error); box-shadow: 0 0 8px var(--error); }
.instance-url { margin-top: 6px; font-size: 11px; color: var(--text-dim); word-break: break-all; }

.examples { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 8px; }
.examples li {
  font-size: 12.5px; color: var(--text-dim);
  padding: 8px 10px; border-radius: 8px;
  background: rgba(255, 255, 255, 0.02);
  border: 1px solid transparent;
  cursor: pointer;
  transition: all 0.15s;
  line-height: 1.4;
}
.examples li:hover {
  background: rgba(122, 183, 255, 0.08);
  border-color: rgba(122, 183, 255, 0.2);
  color: var(--text);
}

/* -------- Chat -------- */

.chat {
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100vh;
  min-width: 0;
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 20px;
  border-bottom: 1px solid var(--panel-border);
  background: rgba(14, 37, 64, 0.4);
  backdrop-filter: blur(8px);
}
.chat-title {
  font-size: 14px;
  color: var(--text-dim);
  font-weight: 500;
}

.icon-btn {
  width: 36px; height: 36px;
  border-radius: 8px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: all 0.15s;
}
.icon-btn:hover { background: rgba(122, 183, 255, 0.1); }
.mobile-only { display: none; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 32px max(24px, calc((100% - 900px) / 2)) 20px;
  scroll-behavior: smooth;
}

.welcome {
  text-align: center;
  padding: 60px 20px 30px;
  color: var(--text);
}
.welcome h2 {
  margin: 0 0 12px;
  font-size: 28px;
  font-weight: 600;
  background: linear-gradient(120deg, #fff 20%, #a7cbff 80%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.welcome p { margin: 0; color: var(--text-dim); font-size: 14px; }

/* -------- Messages -------- */

.msg {
  display: flex;
  gap: 12px;
  margin-bottom: 18px;
  animation: fadeIn 0.3s ease;
  position: relative;
}
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}
.msg.user { justify-content: flex-end; }

.avatar {
  width: 30px; height: 30px; border-radius: 50%; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 600;
}
.msg.assistant .avatar {
  background: linear-gradient(135deg, var(--accent), #a7cbff);
  color: #0a1128;
  box-shadow: 0 0 12px var(--accent-glow);
}

.bubble {
  max-width: 760px;
  padding: 12px 16px;
  border-radius: 14px;
  line-height: 1.6;
  font-size: 14.5px;
  word-wrap: break-word;
  min-width: 0;
}
.msg.user .bubble {
  background: rgba(14, 37, 64, 0.55);
  backdrop-filter: blur(10px);
  color: #fff;
  border: 1px solid var(--panel-border);
  border-bottom-right-radius: 4px;
  box-shadow: 0 6px 20px rgba(14, 37, 64, 0.35);
  white-space: pre-wrap;
}
.msg.assistant .bubble {
  background: var(--bot-bubble);
  border: 1px solid var(--panel-border);
  border-bottom-left-radius: 4px;
}

/* Message actions (copy, etc) — visible on hover */
.msg-actions {
  display: flex;
  gap: 4px;
  margin-top: 4px;
  opacity: 0;
  transition: opacity 0.15s;
}
.msg:hover .msg-actions { opacity: 1; }
.msg.assistant .msg-actions { padding-left: 42px; /* align past avatar */ }
.msg-actions button {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-mute);
  padding: 4px 8px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 11.5px;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  transition: all 0.12s;
}
.msg-actions button:hover {
  background: rgba(122, 183, 255, 0.08);
  color: var(--text);
  border-color: rgba(122, 183, 255, 0.15);
}
.msg-actions button.copied {
  color: var(--success);
  border-color: rgba(53, 208, 161, 0.25);
  background: rgba(53, 208, 161, 0.08);
}

/* -------- Markdown -------- */

.md > *:first-child { margin-top: 0; }
.md > *:last-child { margin-bottom: 0; }
.md p { margin: 8px 0; }
.md h1, .md h2, .md h3, .md h4 { margin: 14px 0 6px; font-weight: 600; line-height: 1.3; }
.md h1 { font-size: 20px; }
.md h2 { font-size: 17px; }
.md h3 { font-size: 15px; }
.md h4 { font-size: 14px; color: var(--accent-2); }
.md ul, .md ol { padding-left: 22px; margin: 8px 0; }
.md li { margin: 3px 0; }
.md code {
  background: rgba(0, 0, 0, 0.32);
  padding: 1px 6px; border-radius: 4px;
  font-size: 13px;
  font-family: "SF Mono", Menlo, Consolas, monospace;
  color: #a7cbff;
}
.md pre {
  background: rgba(0, 0, 0, 0.4);
  border-radius: 10px;
  overflow: hidden;
  margin: 10px 0;
  border: 1px solid rgba(122, 183, 255, 0.1);
  position: relative;
}
.md:not(.streaming) pre {
  overflow: auto;
  max-height: 360px;
}
.md pre code {
  display: block;
  background: transparent;
  padding: 12px 14px;
  color: var(--text);
  font-size: 12.5px;
  font-family: "SF Mono", Menlo, Consolas, monospace;
  overflow-x: auto;
  line-height: 1.5;
}

/* Code block header */
.code-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 12px;
  background: rgba(0, 0, 0, 0.25);
  border-bottom: 1px solid rgba(122, 183, 255, 0.08);
  font-size: 11px;
  color: var(--text-mute);
  text-transform: uppercase;
  letter-spacing: 0.6px;
}
.code-header button {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-mute);
  padding: 3px 8px;
  border-radius: 5px;
  font-size: 11px;
  cursor: pointer;
  transition: all 0.12s;
  text-transform: none;
  letter-spacing: 0;
}
.code-header button:hover {
  background: rgba(122, 183, 255, 0.1);
  color: var(--text);
}
.code-header button.copied {
  color: var(--success);
}

.md blockquote {
  border-left: 3px solid var(--accent);
  margin: 8px 0;
  padding: 4px 12px;
  color: var(--text-dim);
  background: rgba(122, 183, 255, 0.05);
  border-radius: 4px;
}
.md a { color: var(--accent-2); text-decoration: none; border-bottom: 1px dashed var(--accent-2); }
.md a:hover { color: white; border-bottom-style: solid; }
.md hr {
  border: none;
  border-top: 1px solid var(--panel-border);
  margin: 14px 0;
}

/* Markdown tables — while streaming, flow naturally into the outer scroll
   container (no nested scroll — nested scroll fights with the streaming
   re-renders). Once the .streaming class is removed on finalize, tables
   collapse into a compact scrollable widget. */
.md table {
  border-collapse: collapse;
  margin: 12px 0;
  width: 100%;
  font-size: 13.5px;
  background: rgba(0, 0, 0, 0.16);
  border-radius: 8px;
  border: 1px solid rgba(122, 183, 255, 0.14);
  overflow: hidden;
}
.md:not(.streaming) table {
  display: block;
  overflow: auto;
  max-height: 360px;
}
.md:not(.streaming) table thead th {
  position: sticky;
  top: 0;
  background: #1b3a6b;
  z-index: 1;
}
.md thead { background: rgba(77, 140, 255, 0.14); }
.md th, .md td {
  padding: 8px 12px;
  text-align: left;
  border-bottom: 1px solid rgba(122, 183, 255, 0.08);
  vertical-align: top;
}
.md th {
  font-weight: 600;
  color: var(--accent-2);
  font-size: 12.5px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
}
.md tbody tr:last-child td { border-bottom: none; }
.md tbody tr:hover { background: rgba(122, 183, 255, 0.04); }

/* Streaming caret */
.md.streaming::after {
  content: "";
  display: inline-block;
  width: 6px;
  height: 14px;
  vertical-align: text-bottom;
  background: var(--accent-2);
  margin-left: 2px;
  border-radius: 1px;
  animation: caret 1s steps(2) infinite;
}
@keyframes caret {
  50% { opacity: 0; }
}

/* -------- Tool call indicator -------- */

.tool-call {
  margin: 8px 0;
  padding: 8px 12px;
  border-radius: 10px;
  background: rgba(122, 183, 255, 0.06);
  border: 1px solid rgba(122, 183, 255, 0.14);
  font-size: 12.5px;
  color: var(--text-dim);
  display: flex;
  align-items: center;
  gap: 10px;
  animation: fadeIn 0.2s ease;
}
.tool-call.done { color: var(--text); }
.tool-call .spinner {
  width: 12px; height: 12px;
  border: 2px solid rgba(122, 183, 255, 0.3);
  border-top-color: var(--accent-2);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }
.tool-call .check { color: var(--success); font-size: 14px; }
.tool-call .fail { color: var(--error); }
.tool-call code {
  background: rgba(0, 0, 0, 0.25);
  padding: 1px 6px;
  border-radius: 4px;
  font-family: "SF Mono", Menlo, Consolas, monospace;
  font-size: 11.5px;
  color: var(--accent-2);
}

/* -------- Thinking dots -------- */

.thinking {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 0;
}
.thinking .dot-t {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--accent-2);
  opacity: 0.4;
  animation: bounce 1.3s ease-in-out infinite;
}
.thinking .dot-t:nth-child(2) { animation-delay: 0.15s; }
.thinking .dot-t:nth-child(3) { animation-delay: 0.3s; }
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-5px); opacity: 1; }
}

/* -------- Composer -------- */

.composer-wrap {
  padding: 8px max(24px, calc((100% - 900px) / 2)) 14px;
  background: transparent;
}

.composer { display: flex; align-items: flex-end; gap: 10px; }

.composer textarea {
  flex: 1;
  min-height: 44px;
  max-height: 200px;
  resize: none;
  border-radius: 14px;
  border: 1px solid rgba(255, 255, 255, 0.45);
  background: rgba(14, 37, 64, 0.55);
  backdrop-filter: blur(10px);
  padding: 12px 14px;
  color: #fff;
  font-size: 14.5px;
  font-family: inherit;
  line-height: 1.5;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s, background 0.15s;
  overflow-y: auto;
  scrollbar-width: none;
  box-shadow: 0 6px 20px rgba(14, 37, 64, 0.4);
}
.composer textarea::-webkit-scrollbar { display: none; }
.composer textarea:focus {
  border-color: #fff;
  background: rgba(14, 37, 64, 0.7);
  box-shadow: 0 0 0 3px var(--accent-glow), 0 6px 20px rgba(14, 37, 64, 0.4);
}
.composer textarea::placeholder { color: rgba(255, 255, 255, 0.7); }

.composer button,
.attach-btn {
  width: 44px; height: 44px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.45);
  background: rgba(14, 37, 64, 0.55);
  backdrop-filter: blur(10px);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
  flex-shrink: 0;
  box-shadow: 0 6px 20px rgba(14, 37, 64, 0.4);
}
.composer button:hover,
.attach-btn:hover {
  background: rgba(27, 77, 140, 0.7);
  border-color: #fff;
}
#sendBtn {
  position: relative;
}
#sendBtn .stop-icon { display: none; }
#sendBtn.streaming .send-icon { display: none; }
#sendBtn.streaming .stop-icon { display: block; }
#sendBtn.streaming {
  background: rgba(255, 107, 120, 0.35);
  border-color: rgba(255, 107, 120, 0.8);
}
#sendBtn:hover {
  transform: translateY(-1px);
}
#sendBtn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none;
}

.composer-hint {
  text-align: center;
  font-size: 11px;
  color: var(--text-mute);
  margin-top: 8px;
}

/* -------- File chips -------- */

.pending-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}
.pending-attachments:empty { display: none; }

.file-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  background: rgba(77, 140, 255, 0.14);
  border: 1px solid rgba(122, 183, 255, 0.28);
  border-radius: 10px;
  font-size: 12.5px;
  color: var(--text);
  max-width: 280px;
}
.file-chip .file-ico {
  width: 24px; height: 24px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border-radius: 6px;
  color: white;
  display: flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 700;
  flex-shrink: 0;
}
.file-chip .file-info { display: flex; flex-direction: column; min-width: 0; }
.file-chip .file-name {
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.file-chip .file-meta { font-size: 11px; color: var(--text-dim); }
.file-chip .file-remove {
  width: 20px; height: 20px;
  border-radius: 5px;
  background: transparent;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 15px; line-height: 1;
  padding: 0;
  transition: all 0.15s;
  flex-shrink: 0;
}
.file-chip .file-remove:hover {
  background: rgba(255, 107, 120, 0.18);
  color: var(--error);
}

.msg.user .attachments-inline {
  display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px;
}
.msg.user .attachments-inline .file-chip {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(63, 169, 224, 0.4);
  color: #fff;
}
.msg.user .attachments-inline .file-chip .file-meta { color: rgba(255, 255, 255, 0.7); }

/* -------- Scroll-to-bottom pill -------- */

.scroll-to-bottom {
  position: absolute;
  bottom: 130px;
  left: 50%;
  transform: translateX(-50%) translateY(8px);
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 1px solid var(--panel-border);
  background: rgba(14, 37, 64, 0.85);
  backdrop-filter: blur(8px);
  color: var(--text);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.18s;
  opacity: 0;
  pointer-events: none;
  z-index: 5;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}
.scroll-to-bottom.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
  pointer-events: auto;
}
.scroll-to-bottom:hover {
  background: rgba(122, 183, 255, 0.2);
  border-color: var(--accent-2);
}

/* -------- Dropzone -------- */

.dropzone {
  position: absolute;
  inset: 0;
  background: rgba(14, 37, 64, 0.85);
  backdrop-filter: blur(6px);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 100;
  pointer-events: none;
}
.dropzone.active { display: flex; }
.dropzone-inner {
  padding: 40px 60px;
  border: 2px dashed var(--accent-2);
  border-radius: 20px;
  font-size: 18px;
  color: var(--accent-2);
  background: rgba(122, 183, 255, 0.08);
}

/* Scrollbars */
.messages::-webkit-scrollbar,
.sidebar::-webkit-scrollbar,
.md pre::-webkit-scrollbar,
.md table::-webkit-scrollbar { width: 8px; height: 8px; }
.messages::-webkit-scrollbar-thumb,
.sidebar::-webkit-scrollbar-thumb,
.md pre::-webkit-scrollbar-thumb,
.md table::-webkit-scrollbar-thumb {
  background: rgba(122, 183, 255, 0.2);
  border-radius: 4px;
}
.messages::-webkit-scrollbar-thumb:hover,
.sidebar::-webkit-scrollbar-thumb:hover,
.md pre::-webkit-scrollbar-thumb:hover,
.md table::-webkit-scrollbar-thumb:hover {
  background: rgba(122, 183, 255, 0.35);
}

/* -------- Responsive -------- */

@media (max-width: 800px) {
  .app { grid-template-columns: 1fr; }
  .mobile-only { display: flex; }
  .sidebar {
    position: fixed;
    top: 0; left: 0;
    height: 100vh;
    width: 300px;
    z-index: 200;
    transform: translateX(-100%);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(2px);
    z-index: 150;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
  }
  .sidebar-backdrop.visible { opacity: 1; pointer-events: auto; }
  .welcome h2 { font-size: 22px; }
  .bubble { font-size: 14px; }
}
