:root {
  color-scheme: dark;
  --bg: #0b0d12;
  --panel: #11141b;
  --panel-2: #171b24;
  --line: #272d3a;
  --text: #edf1f7;
  --muted: #8993a5;
  --accent: #38bdf8;
  --accent-2: #0284c7;
  --danger: #ef4444;
  --success: #22c55e;
  --warning: #f59e0b;
}

* { box-sizing: border-box; }
html, body { height: 100%; min-height: 100%; }
body {
  margin: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button, input, textarea, select { font: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }

.login-view {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 20px;
  background:
    radial-gradient(circle at 15% 15%, rgba(56,189,248,.12), transparent 30%),
    radial-gradient(circle at 85% 80%, rgba(34,197,94,.08), transparent 30%),
    var(--bg);
}
.login-card {
  width: min(430px, 100%);
  padding: 34px;
  border: 1px solid var(--line);
  border-radius: 22px;
  background: rgba(17,20,27,.92);
  box-shadow: 0 22px 80px rgba(0,0,0,.38);
}
.brand-mark {
  width: 48px;
  height: 48px;
  display: grid;
  place-items: center;
  border-radius: 14px;
  background: linear-gradient(135deg, var(--accent), #22c55e);
  color: #041017;
  font-weight: 900;
}
.login-card h1 { margin: 18px 0 8px; }
.login-card p { margin: 0 0 24px; color: var(--muted); }
label {
  display: grid;
  gap: 8px;
  margin: 15px 0;
  color: #c7ceda;
  font-size: 14px;
}
input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 11px;
  outline: none;
  color: var(--text);
  background: #0e1117;
}
input, select { height: 44px; padding: 0 13px; }
textarea { padding: 13px 14px; resize: none; line-height: 1.5; }
input:focus, select:focus, textarea:focus { border-color: var(--accent-2); box-shadow: 0 0 0 3px rgba(56,189,248,.1); }

.primary-btn, .danger-btn, .ghost-btn {
  min-height: 42px;
  border: 0;
  border-radius: 11px;
  padding: 0 16px;
  font-weight: 700;
}
.primary-btn { background: var(--accent); color: #041017; }
.primary-btn:hover { background: #7dd3fc; }
.primary-btn:disabled { opacity: .45; cursor: not-allowed; }
.danger-btn { background: rgba(239,68,68,.14); color: #fca5a5; border: 1px solid rgba(239,68,68,.35); }
.ghost-btn { width: 100%; background: #1a1f29; color: var(--text); }
.form-error { min-height: 22px; color: #fca5a5; font-size: 14px; }

.app { height: 100dvh; min-height: 0; display: grid; grid-template-columns: 290px 1fr; }
.sidebar {
  min-width: 0;
  border-right: 1px solid var(--line);
  background: var(--panel);
  display: grid;
  grid-template-rows: auto auto 1fr auto;
}
.sidebar-head, .topbar {
  height: 68px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid var(--line);
}
.sidebar-head { padding: 0 16px; justify-content: space-between; }
.sidebar-head strong { display: block; }
.sidebar-head small { display: block; color: var(--muted); margin-top: 3px; }
.new-chat-box { padding: 14px; display: grid; gap: 10px; border-bottom: 1px solid var(--line); }
.folder-picker {
  display: grid;
  gap: 8px;
  padding: 9px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: #0d1016;
}
.folder-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
}
#folderPathInput {
  height: 34px;
  padding: 0 10px;
  border-radius: 8px;
  font-size: 12px;
}
#folderPath {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: #cbd3df;
  font-size: 12px;
}
.mini-btn {
  min-height: 28px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 10px;
  background: #171b24;
  color: var(--text);
  font-size: 12px;
}
.mini-btn:disabled { opacity: .45; cursor: not-allowed; }
.conversation-list { padding: 9px; overflow-y: auto; }
.conversation-item {
  position: relative;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  padding: 11px;
  border-radius: 11px;
  margin-bottom: 5px;
  color: #cbd3df;
}
.conversation-item:hover, .conversation-item.active { background: var(--panel-2); color: white; }
.conversation-main { min-width: 0; cursor: pointer; }
.conversation-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.conversation-meta { margin-top: 5px; color: var(--muted); font-size: 11px; }
.conversation-menu {
  border: 0;
  background: transparent;
  color: var(--muted);
  border-radius: 8px;
  width: 30px;
}
.conversation-menu:hover { background: #242a36; color: white; }
.sidebar-footer { padding: 12px; border-top: 1px solid var(--line); }

.main { min-width: 0; min-height: 0; height: 100%; display: grid; grid-template-rows: auto minmax(0, 1fr) auto auto; }
.topbar { padding: 0 18px; gap: 13px; }
.chat-heading { min-width: 0; flex: 1; }
.chat-heading strong, .chat-heading span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-heading span { color: var(--muted); font-size: 12px; margin-top: 4px; }
.status-badge {
  border-radius: 999px;
  padding: 6px 10px;
  font-size: 12px;
  border: 1px solid var(--line);
  color: var(--muted);
}
.status-badge.running { color: #fde68a; border-color: rgba(245,158,11,.4); background: rgba(245,158,11,.1); }
.status-badge.failed { color: #fca5a5; border-color: rgba(239,68,68,.4); }
.status-badge.completed { color: #86efac; border-color: rgba(34,197,94,.4); }

.messages { min-height: 0; overflow-y: auto; padding: 28px max(22px, calc((100% - 920px) / 2)); }
.empty-state { height: 100%; display: grid; place-content: center; justify-items: center; text-align: center; color: var(--muted); }
.empty-state h2 { color: var(--text); margin-bottom: 4px; }
.message { display: grid; grid-template-columns: 38px 1fr; gap: 13px; margin-bottom: 25px; }
.avatar {
  width: 36px; height: 36px; border-radius: 11px; display: grid; place-items: center;
  background: #242a35; font-size: 12px; font-weight: 800;
}
.message.user .avatar { background: #164e63; color: #a5f3fc; }
.message.assistant .avatar { background: #14532d; color: #bbf7d0; }
.message.system .avatar { background: #4c1d1d; color: #fecaca; }
.message-head { font-size: 13px; font-weight: 800; margin: 2px 0 8px; }
.message-content { color: #d6dce6; line-height: 1.65; white-space: pre-wrap; overflow-wrap: anywhere; }
.message-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}
.image-chip {
  display: inline-grid;
  grid-template-columns: 42px minmax(0, 130px);
  gap: 8px;
  align-items: center;
  min-height: 46px;
  padding: 4px 9px 4px 4px;
  border: 1px solid var(--line);
  border-radius: 10px;
  color: #d6dce6;
  text-decoration: none;
  background: #10141c;
}
.image-chip img {
  width: 42px;
  height: 38px;
  object-fit: cover;
  border-radius: 7px;
  background: #05070a;
}
.image-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; }
.message-time { margin-top: 7px; font-size: 11px; color: var(--muted); }

.live-panel {
  margin: 0 max(14px, calc((100% - 960px) / 2));
  border: 1px solid var(--line);
  border-bottom: 0;
  border-radius: 13px 13px 0 0;
  background: #090b0f;
  max-height: min(24dvh, 210px);
  overflow: hidden;
}
.live-head { height: 38px; padding: 0 12px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--line); }
.live-head strong { font-size: 12px; }
.text-btn { border: 0; background: transparent; color: var(--muted); }
.live-panel pre {
  margin: 0; padding: 12px; overflow: auto; max-height: min(20dvh, 170px);
  color: #a7f3d0; font-size: 12px; line-height: 1.55; white-space: pre-wrap;
}

.composer {
  padding: 14px max(14px, calc((100% - 960px) / 2)) 18px;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: end;
  min-height: 0;
  max-height: min(34dvh, 300px);
  overflow: hidden;
  border-top: 1px solid var(--line);
  background: rgba(11,13,18,.96);
}
.composer textarea {
  min-height: 46px;
  height: 46px;
  max-height: min(20dvh, 180px);
  overflow-y: auto;
  overscroll-behavior: contain;
}
.attachment-preview {
  grid-column: 1 / -1;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.pending-image {
  display: inline-grid;
  grid-template-columns: 36px minmax(0, 110px);
  gap: 7px;
  align-items: center;
  max-width: 180px;
  min-height: 42px;
  padding: 4px 8px 4px 4px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #151a23;
  color: #d6dce6;
}
.pending-image img {
  width: 36px;
  height: 34px;
  object-fit: cover;
  border-radius: 7px;
}
.pending-image span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; }
.composer-actions { display: flex; gap: 8px; }
.ghost-square {
  min-width: 50px;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: #1a1f29;
  color: var(--text);
  font-weight: 700;
}
.database-panel {
  position: fixed;
  inset: 0 0 0 auto;
  z-index: 80;
  width: min(720px, 100vw);
  display: grid;
  grid-template-rows: auto 1fr;
  border-left: 1px solid var(--line);
  background: #0b0d12;
  box-shadow: -24px 0 80px rgba(0,0,0,.48);
}
.database-head {
  height: 68px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 0 16px;
  border-bottom: 1px solid var(--line);
}
.database-head strong, .database-head span { display: block; }
.database-head span { color: var(--muted); font-size: 12px; margin-top: 3px; }
.database-body {
  overflow: auto;
  padding: 16px;
  display: grid;
  align-content: start;
  gap: 12px;
}
.db-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.db-result {
  min-height: 260px;
  max-height: 46vh;
  overflow: auto;
  margin: 0;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: #07090d;
  color: #c7f9d4;
  font-size: 12px;
  line-height: 1.55;
  white-space: pre;
}
.icon-btn {
  border: 0; color: var(--text); background: #1a1f29; border-radius: 9px;
  width: 38px; height: 38px; font-size: 19px;
}
.toast {
  position: fixed; right: 20px; bottom: 20px; z-index: 100;
  max-width: 420px; padding: 12px 15px; border-radius: 11px;
  background: #252b36; box-shadow: 0 12px 35px rgba(0,0,0,.35);
  opacity: 0; pointer-events: none; transform: translateY(10px); transition: .2s;
}
.toast.show { opacity: 1; transform: translateY(0); }
.mobile-only { display: none; }

@media (max-width: 760px) {
  .mobile-only { display: inline-grid; place-items: center; }
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: min(88vw, 330px); z-index: 50;
    transform: translateX(-105%); transition: transform .2s ease;
    box-shadow: 20px 0 60px rgba(0,0,0,.55);
  }
  .sidebar.open { transform: translateX(0); }
  .topbar { height: 61px; padding: 0 11px; }
  .messages { padding: 20px 14px; }
  .live-panel {
    max-height: 96px;
  }
  .live-panel pre {
    max-height: 58px;
  }
  .composer {
    grid-template-columns: minmax(0, 1fr);
    padding: 10px max(10px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
    max-height: 34dvh;
  }
  .composer textarea { max-height: 18dvh; }
  .composer-actions { justify-content: flex-end; flex-wrap: wrap; }
  .composer-actions > button { flex: 0 0 auto; }
  .live-panel { margin: 0 10px; }
  .status-badge { display: none; }
  .database-panel { width: 100vw; }
}
