:root {
  --bg: #0b0f14;
  --surface: #11161d;
  --surface-2: #161c25;
  --border: #232b36;
  --border-strong: #2f3947;
  --text: #e6e9ee;
  --text-muted: #8b95a3;
  --accent: #3b82f6;
  --accent-hover: #2f6fe0;
  --good: #16a34a;
  --warn: #d97706;
  --bad: #dc2626;
}

* { box-sizing: border-box; }

body {
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  letter-spacing: -0.01em;
}

/* Nav */
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 8px 12px;
  border-radius: 8px;
  color: var(--text-muted);
  font-size: 0.875rem;
  font-weight: 500;
  transition: background 0.12s, color 0.12s;
}
.nav-item:hover { background: var(--surface-2); color: var(--text); }
.nav-item.active { background: var(--surface-2); color: var(--text); }
.nav-item svg { width: 18px; height: 18px; stroke-width: 1.75; flex-shrink: 0; }

/* Chat */
.msg-row { display: flex; }
.msg-row.user { justify-content: flex-end; }
.bubble {
  max-width: 78%;
  padding: 10px 14px;
  border-radius: 12px;
  line-height: 1.6;
  word-break: break-word;
}
/* User bubbles: plain text, preserve newlines */
.bubble.user { background: var(--accent); color: #fff; border-bottom-right-radius: 4px; white-space: pre-wrap; }
/* Assistant bubbles: markdown-rendered HTML, let block elements breathe */
.bubble.assistant { background: var(--surface-2); border: 1px solid var(--border); border-bottom-left-radius: 4px; }

/* ---- Markdown styles inside assistant bubbles ---- */
.bubble.assistant p { margin: 0 0 0.6em; }
.bubble.assistant p:last-child { margin-bottom: 0; }
.bubble.assistant h1, .bubble.assistant h2, .bubble.assistant h3,
.bubble.assistant h4, .bubble.assistant h5, .bubble.assistant h6 {
  font-weight: 600; margin: 0.75em 0 0.3em; line-height: 1.3;
}
.bubble.assistant h1 { font-size: 1.15em; }
.bubble.assistant h2 { font-size: 1.05em; }
.bubble.assistant h3 { font-size: 0.95em; }
.bubble.assistant ul, .bubble.assistant ol {
  margin: 0.4em 0 0.6em 1.25em; padding: 0;
}
.bubble.assistant li { margin-bottom: 0.2em; }
.bubble.assistant li > ul, .bubble.assistant li > ol { margin: 0.15em 0 0.15em 1em; }
.bubble.assistant strong { font-weight: 600; }
.bubble.assistant em { font-style: italic; }
.bubble.assistant code {
  font-family: "SF Mono", "Fira Code", Consolas, "Courier New", monospace;
  font-size: 0.82em;
  background: rgba(255,255,255,0.07);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
}
.bubble.assistant pre {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  overflow-x: auto;
  margin: 0.5em 0;
}
.bubble.assistant pre code {
  background: none; border: none; padding: 0;
  font-size: 0.82em; line-height: 1.5;
}
.bubble.assistant blockquote {
  border-left: 3px solid var(--border-strong);
  margin: 0.5em 0;
  padding: 0.2em 0.75em;
  color: var(--text-muted);
}
.bubble.assistant hr {
  border: none; border-top: 1px solid var(--border); margin: 0.6em 0;
}
.bubble.assistant a { color: var(--accent); text-decoration: underline; }
.bubble.assistant table { border-collapse: collapse; width: 100%; margin: 0.5em 0; font-size: 0.875em; }
.bubble.assistant th { font-weight: 600; border-bottom: 1px solid var(--border-strong); padding: 5px 8px; text-align: left; }
.bubble.assistant td { padding: 4px 8px; border-bottom: 1px solid var(--border); }
.bubble.assistant tr:last-child td { border-bottom: none; }
.event-line {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.75rem;
  color: var(--text-muted);
  padding: 2px 0;
}
.event-line svg { width: 13px; height: 13px; }

/* Session history list */
.sess-item { background: transparent; color: var(--text); border: none; cursor: pointer; }
.sess-item:hover { background: var(--surface); }

/* Chat image attachments */
.bubble-imgs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.bubble-imgs img {
  max-width: 220px;
  max-height: 220px;
  border-radius: 8px;
  display: block;
}
.chat-thumb { position: relative; width: 56px; height: 56px; }
.chat-thumb img {
  width: 56px; height: 56px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--border);
}
.chat-thumb-x {
  position: absolute; top: -6px; right: -6px;
  width: 18px; height: 18px;
  border-radius: 50%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  display: inline-flex; align-items: center; justify-content: center;
  cursor: pointer; padding: 0;
}
.chat-thumb-x svg { width: 11px; height: 11px; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border-radius: 8px;
  font-weight: 500;
  font-size: 0.875rem;
  transition: background 0.12s, border-color 0.12s;
  cursor: pointer;
  border: none;
}
.btn-primary { background: var(--accent); color: #fff; padding: 8px 16px; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.55; cursor: default; }
.btn-secondary { background: var(--surface-2); color: var(--text-muted); padding: 8px 14px; border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--border); color: var(--text); }
.btn-secondary:disabled { opacity: 0.55; cursor: default; }
.btn-danger { background: transparent; color: var(--bad); padding: 8px 14px; border: 1px solid var(--bad); }
.btn-danger:hover { background: var(--bad); color: #fff; }
.btn-ghost { background: transparent; color: var(--text-muted); padding: 8px 12px; border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }

.icon-btn { color: var(--text-muted); transition: color 0.12s; line-height: 0; background: transparent; border: none; cursor: pointer; }
.icon-btn:hover { color: var(--bad); }
.icon-btn svg { width: 16px; height: 16px; }

/* Inputs */
.field {
  width: 100%;
  padding: 9px 12px;
  border-radius: 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 0.875rem;
  transition: border-color 0.12s;
}
.field:focus { outline: none; border-color: var(--accent); }
.field::placeholder { color: #5c6572; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; }

/* Chat typing indicator */
.typing-bubble { display: inline-flex; align-items: center; gap: 4px; padding: 12px 14px; }
.typing-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--text-muted);
  animation: typing-bounce 1.2s infinite ease-in-out;
}
.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-4px); opacity: 1; }
}

table.data { width: 100%; font-size: 0.875rem; border-collapse: collapse; }
table.data thead { color: var(--text-muted); font-weight: 500; }
table.data th { text-align: left; padding: 10px 12px; font-weight: 500; border-bottom: 1px solid var(--border); }
table.data td { padding: 10px 12px; border-bottom: 1px solid var(--border); }
table.data tbody tr:last-child td { border-bottom: none; }
table.data tbody tr:hover { background: var(--surface-2); }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 5px; }
::-webkit-scrollbar-track { background: transparent; }

/* ---- Mobile: sidebar becomes a bottom tab bar ---- */
@media (max-width: 767px) {
  .app-shell { flex-direction: column; }
  .sidebar {
    order: 2;
    width: 100% !important;
    flex-direction: row !important;
    border-right: none !important;
    border-top: 1px solid var(--border);
    height: auto;
  }
  .sidebar-header { display: none; }
  .sidebar-footer {
    border-top: none !important;
    padding: 6px !important;
    display: flex;
    align-items: center;
  }
  .sidebar-footer > div:first-child { display: none; }
  .sidebar-footer #logout { width: auto; padding: 8px; }
  .sidebar-footer #logout span { display: none; }
  .sidebar-nav {
    display: flex;
    flex-direction: row;
    overflow-x: auto;
    flex: 1;
    gap: 2px;
    padding: 4px !important;
  }
  .sidebar-nav .nav-item {
    flex-direction: column;
    gap: 3px;
    padding: 6px 8px;
    font-size: 0.65rem;
    white-space: nowrap;
    min-width: 56px;
    justify-content: center;
  }
  .main-area { order: 1; flex: 1; min-height: 0; }
  .bubble { max-width: 88%; }
}

/* ── Undo Toast ─────────────────────────────────────────────────── */
#undo-toast-root {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 9999;
  pointer-events: none;
}

.undo-toast {
  display: flex;
  align-items: center;
  gap: 12px;
  background: #1e2730;
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  padding: 12px 16px 8px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.5);
  min-width: 260px;
  max-width: 420px;
  pointer-events: all;
  opacity: 0;
  transform: translateY(12px);
  transition: opacity 0.18s ease, transform 0.18s ease;
  flex-wrap: wrap;
  row-gap: 6px;
}

.undo-toast--visible {
  opacity: 1;
  transform: translateY(0);
}

.undo-toast__label {
  flex: 1;
  font-size: 0.875rem;
  color: var(--text);
}

.undo-toast__btn {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--accent);
  background: none;
  border: none;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 4px;
  transition: background 0.12s;
}
.undo-toast__btn:hover { background: rgba(59,130,246,0.15); }

.undo-toast__bar {
  width: 100%;
  height: 3px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
}

.undo-toast__bar-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 2px;
  width: 100%;
}
