/* Theme tokens */
:root {
  --bg: #f7f7f9;
  --surface: #ffffff;
  --surface-2: #f0f0f3;
  --border: #e4e4e9;
  --text: #18181b;
  --text-muted: #6f6f78;
  --accent: #6366f1;
  --accent-hover: #4f46e5;
  --accent-fg: #ffffff;
  --accent-ring: rgba(99, 102, 241, 0.16);
  --user-bubble: #eef1ff;
  --assistant-bubble: #f2f2f5;
  --danger: #e11d48;
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
[data-theme="dark"] {
  --bg: #0a0a0d;
  --surface: #131317;
  --surface-2: #1c1c22;
  --border: #26262d;
  --text: #ececee;
  --text-muted: #9a9aa5;
  --accent: #818cf8;
  --accent-hover: #a5b4fc;
  --accent-fg: #0a0a0d;
  --accent-ring: rgba(129, 140, 248, 0.25);
  --user-bubble: #2d2d47;
  --assistant-bubble: #1c1c22;
  --danger: #fb7185;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  transition: background 0.2s ease, color 0.2s ease;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }

/* App shell */
.app { display: flex; height: 100vh; }

/* Sidebar */
.sidebar {
  width: 264px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 14px 12px 12px;
  gap: 10px;
  overflow: hidden;
  transition: width 0.2s ease, padding 0.2s ease, border 0.2s ease;
}
.app.sidebar-collapsed .sidebar {
  width: 0;
  padding-left: 0;
  padding-right: 0;
  border-right: none;
}
.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 2px 4px 10px;
}
.logo { display: inline-flex; align-items: center; gap: 8px; }
.logo-mark { width: 20px; height: 20px; color: var(--accent); flex-shrink: 0; }
.logo-text { font-weight: 700; font-size: 15px; letter-spacing: -0.01em; }
.icon-btn {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  width: 32px;
  height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  color: var(--text-muted);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

.new-chat-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 10px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface-2);
  font-weight: 600;
  font-size: 14px;
  text-align: left;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.new-chat-btn:hover { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }

.conv-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.conv-item {
  padding: 9px 11px;
  border-radius: 8px;
  font-size: 13.5px;
  color: var(--text-muted);
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background 0.12s, color 0.12s;
}
.conv-item:hover { background: var(--surface-2); color: var(--text); }
.conv-item.active { background: var(--surface-2); color: var(--text); font-weight: 500; }
.conv-empty { color: var(--text-muted); font-size: 13px; padding: 8px 11px; }

.sidebar-footer {
  padding: 10px 0 0;
  border-top: 1px solid var(--border);
  font-size: 13px;
}
.sidebar-footer a { color: var(--text-muted); text-decoration: none; padding: 4px; }
.sidebar-footer a:hover { color: var(--text); }
.link-btn { color: var(--text-muted); background: none; border: none; padding: 4px; font-size: 13px; }
.link-btn:hover { color: var(--text); }

.temp-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 11px;
  font-size: 13.5px;
  color: var(--text-muted);
  cursor: pointer;
  border-radius: 8px;
  user-select: none;
}
.temp-toggle:hover { background: var(--surface-2); color: var(--text); }
.temp-toggle input { accent-color: var(--accent); }

.user-menu { position: relative; width: 100%; }
.user-btn {
  display: flex;
  align-items: center;
  gap: 9px;
  width: 100%;
  padding: 8px 10px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  font-size: 14px;
}
.user-btn:hover { background: var(--surface-2); }
.avatar {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-fg);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  flex-shrink: 0;
}
.user-name { flex: 1; text-align: left; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chevron { color: var(--text-muted); font-size: 11px; }
.user-popup {
  position: absolute;
  bottom: 100%;
  left: 0;
  margin-bottom: 6px;
  width: 190px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
  padding: 4px;
  z-index: 10;
}
.popup-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 8px 11px;
  border: none;
  border-radius: 7px;
  background: transparent;
  color: var(--text);
  font-size: 14px;
}
.popup-item:hover { background: var(--surface-2); }
.popup-item.danger { color: var(--danger); }

/* Empty state (centered input) */
.welcome { display: none; text-align: center; padding: 0 24px 20px; }
.welcome h2 { margin: 0 0 8px; font-size: 26px; letter-spacing: -0.02em; }
.welcome-sub { margin: 0; color: var(--text-muted); font-size: 14.5px; }
.composer { flex-shrink: 0; }
.main.empty .welcome { display: block; }
.main.empty .messages { display: none; }
.main.empty .composer {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.main.empty #message { min-height: 56px; }

/* Non-empty: composer floats over the messages with a fade halo */
.main:not(.empty) .composer {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 5;
  padding-top: 56px;
  background: linear-gradient(to bottom, transparent, var(--bg) 56px);
}
.main:not(.empty) .messages-inner {
  padding-bottom: 220px;
}

.attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  max-width: 880px;
  width: 100%;
  margin: 0 auto 8px;
  padding: 0 24px;
}
.attach-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  font-size: 13px;
  color: var(--text);
}
.chip-x {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 15px;
  line-height: 1;
  padding: 0 2px;
  cursor: pointer;
}
.chip-x:hover { color: var(--text); }

/* Megamind refinements */
#sidebar-toggle svg { width: 16px; height: 16px; transition: transform 0.2s ease; }
.app.sidebar-collapsed #sidebar-toggle svg { transform: rotate(180deg); }

.nc-icon { width: 16px; height: 16px; flex-shrink: 0; }

.user-row { display: flex; align-items: center; gap: 4px; }
.user-row .user-menu { flex: 1; }

.header-left { display: flex; align-items: center; }
.temp-btn svg { width: 18px; height: 18px; }
.temp-btn.active { color: var(--accent); border-color: var(--accent); background: var(--accent-ring); }

.attach-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 6px;
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.attach-btn svg { width: 18px; height: 18px; }
.attach-btn:hover { color: var(--text); background: var(--surface-2); }

/* Collapsed thin rail */
.app.sidebar-collapsed .sidebar { width: 64px; padding: 14px 8px 12px; }
.app.sidebar-collapsed .sidebar-header { flex-direction: column; gap: 14px; }
.app.sidebar-collapsed .logo-text,
.app.sidebar-collapsed .nc-text,
.app.sidebar-collapsed .conv-list,
.app.sidebar-collapsed .user-name { display: none; }
.app.sidebar-collapsed .new-chat-btn { justify-content: center; padding: 10px; }
.app.sidebar-collapsed .user-row { flex-direction: column; }
.app.sidebar-collapsed .user-btn { justify-content: center; }
.app.sidebar-collapsed .sidebar-footer { margin-top: auto; }

/* Main chat column */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }
.chat-header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 53px;
}
.context-info { color: var(--text-muted); font-size: 12.5px; }

.messages { flex: 1; overflow-y: auto; scroll-behavior: smooth; }
.messages-inner { max-width: 880px; margin: 0 auto; padding: 24px 24px 8px; }

.msg { margin-bottom: 16px; animation: fadein 0.25s ease; }
.msg.user { text-align: right; }
.bubble {
  display: inline-block;
  max-width: 82%;
  padding: 10px 14px;
  border-radius: 14px;
  text-align: left;
  line-height: 1.55;
  font-size: 15px;
  white-space: pre-wrap;
}
.msg.assistant .bubble { background: var(--assistant-bubble); border-bottom-left-radius: 4px; }
.msg.user .bubble { background: var(--user-bubble); border-bottom-right-radius: 4px; }
.bubble.md ul, .bubble.md ol { margin: 4px 0 4px 20px; padding: 0; }
.bubble.md code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; }
.bubble.md pre { background: var(--surface-2); padding: 10px 12px; border-radius: 8px; overflow-x: auto; }
.bubble.md h1, .bubble.md h2, .bubble.md h3 { margin: 8px 0 4px; }
.bubble.md p { margin: 4px 0; }
.bubble.md table { border-collapse: collapse; margin: 6px 0; }
.bubble.md th, .bubble.md td { border: 1px solid var(--border); padding: 4px 9px; font-size: 13px; }

.status { color: var(--text-muted); font-size: 13.5px; padding: 4px 2px; display: flex; align-items: center; gap: 8px; }
.status .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { opacity: 0.2; } 50% { opacity: 1; } }
@keyframes fadein { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.msg.thinking .bubble { background: transparent; color: var(--text-muted); font-style: italic; font-size: 13.5px; padding: 2px 0; }

.tool.confirm {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
  margin: 8px 0;
  background: var(--surface);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  font-size: 14px;
  max-width: 82%;
}
.tool.confirm button { padding: 7px 14px; border: none; border-radius: 8px; background: var(--accent); color: var(--accent-fg); font-weight: 600; }
.tool.confirm button:hover { background: var(--accent-hover); }
.tool.confirm button:disabled { opacity: 0.6; cursor: default; }

.chat-form {
  max-width: 880px;
  width: 100%;
  margin: 0 auto;
  padding: 12px 24px 20px;
}
.composer-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.composer-box:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-ring);
}
.composer-options {
  display: flex;
  gap: 6px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
}
.pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
}
.pill-icon { width: 14px; height: 14px; color: var(--text-muted); flex-shrink: 0; }
.pill select {
  border: none;
  background: var(--surface-2);
  font-size: 13px;
  color: var(--text);
  padding: 0;
  cursor: pointer;
  max-width: 200px;
}
.pill select:focus { outline: none; }
.pill select option {
  background: var(--surface);
  color: var(--text);
}
.pill-spinner {
  display: inline-block;
  width: 12px;
  height: 12px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}
.pill-spinner[hidden] { display: none; }
.composer-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
}
#message {
  border: none;
  background: transparent;
  resize: none;
  font-size: 15px;
  line-height: 1.5;
  padding: 8px 4px;
  min-height: 40px;
  max-height: 200px;
  overflow-y: auto;
  transition: min-height 0.2s ease;
}
@keyframes spin { to { transform: rotate(360deg); } }
#message:focus { outline: none; }
.send-btn {
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-fg);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s;
}
.send-btn svg { width: 18px; height: 18px; }
.send-btn:hover { background: var(--accent-hover); }
.send-btn:disabled { opacity: 0.5; cursor: default; }

/* Login */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 36px 32px;
  max-width: 380px;
  margin: 12vh auto 0;
}
.card h1 { margin: 0 0 22px; text-align: center; font-size: 22px; letter-spacing: -0.02em; }
.card input {
  display: block;
  width: 100%;
  margin: 0 0 12px;
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
}
.card input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-ring); }
.card button[type="submit"] {
  width: 100%;
  padding: 11px;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--accent-fg);
  font-weight: 600;
}
.card button[type="submit"]:hover { background: var(--accent-hover); }
.error { color: var(--danger); }
.hint { color: var(--text-muted); font-size: 13px; text-align: center; }

/* Report page */
.page { max-width: 760px; margin: 0 auto; padding: 24px; }
.page-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; }
.page-header h1 { margin: 0; font-size: 20px; }
.page label { display: block; font-size: 13px; color: var(--text-muted); margin-bottom: 6px; }
.page input, .page select {
  width: 100%;
  margin: 0 0 14px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
.page button[type="submit"] {
  padding: 10px 18px;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--accent-fg);
  font-weight: 600;
}
#output {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  white-space: pre-wrap;
  font-size: 13px;
  overflow-x: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

/* Admin page */
.admin-page { max-width: 760px; margin: 0 auto; padding: 24px; }
.admin-page .page-header a { color: var(--text-muted); text-decoration: none; font-size: 14px; }
.admin-page .page-header a:hover { color: var(--text); }
.admin-card { max-width: none; margin: 0 0 20px; }
.admin-card h2 { margin: 0 0 14px; font-size: 16px; }
.admin-card form { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.admin-card form input {
  flex: 1;
  min-width: 160px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
}
.admin-card form button {
  padding: 10px 18px;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--accent-fg);
  font-weight: 600;
}
.admin-card form button.secondary {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
}
.admin-card form select {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
}
.conn-row { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.conn-row span { flex: 1; font-size: 14px; }
.conn-row button {
  padding: 5px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  font-size: 13px;
}
.conn-row button.danger { color: var(--danger); }
.wildcard { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--text-muted); }
.perm-row { display: flex; gap: 16px; margin-bottom: 10px; }
.perm-row label { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--text-muted); }
.scope-inputs { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.scope-row { display: flex; align-items: center; gap: 10px; }
.scope-row label { width: 120px; font-size: 13px; color: var(--text-muted); }
.scope-row input {
  flex: 1;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
}
.muted { color: var(--text-muted); font-size: 13px; }

/* Users table + access dialog */
.table-wrap { overflow-x: auto; margin-top: 12px; }
.users-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.users-table th, .users-table td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); }
.users-table th { color: var(--text-muted); font-weight: 500; font-size: 12.5px; }
.users-table .cell-actions { text-align: right; white-space: nowrap; }
.users-table .cell-actions button {
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  font-size: 13px;
  margin-left: 6px;
}
.users-table .cell-actions button:hover { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.users-table .cell-actions button.danger { color: var(--danger); }
.users-table .cell-actions button.danger:hover { background: var(--danger); color: #fff; border-color: var(--danger); }

.dialog-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 20px;
}
.dialog-backdrop[hidden] { display: none; }
.dialog {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  width: 100%;
  max-width: 480px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);
}
.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.dialog-head h3 { margin: 0; font-size: 15px; }
.dialog-body { padding: 16px; overflow-y: auto; }
.dialog-foot {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}
.dialog-foot button {
  padding: 8px 16px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-fg);
  font-weight: 600;
}
.dialog-foot button.secondary {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
}

/* Admin activity dashboard */
.admin-card-head { display: flex; align-items: center; justify-content: space-between; }
.admin-card-head h2 { margin: 0; }
.activity-stats { display: flex; gap: 10px; margin-bottom: 12px; }
.stat-box { flex: 1; border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; }
.stat-value { font-size: 20px; font-weight: 700; }
.stat-label { font-size: 12px; color: var(--text-muted); }
.by-tool { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.tool-chip { padding: 3px 10px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--border); font-size: 12.5px; }
.activity-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.activity-table th, .activity-table td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); }
.activity-table th { color: var(--text-muted); font-weight: 500; }
.activity-table .error-row td { color: var(--danger); }
