/* Custom Bot — Grok-style monochrome dark UI, LTR English, dependency-free.
   Near-black palette, hairline borders, one restrained accent (white);
   red/amber appear only where they carry meaning (errors, deny, pending). */
:root {
  --bg: #000000;
  --bg-soft: #0b0b0c;
  --panel: #131314;
  --panel2: #1a1a1b;
  --panel3: #26262a;
  --code: #111111;
  --border: rgba(255, 255, 255, 0.08);
  --border-strong: rgba(255, 255, 255, 0.16);
  --text: #f5f5f5;
  --muted: #9a9aa0;
  --accent: #f5f5f5;
  --on-accent: #000000;
  --danger: #e88b8b;
  --danger-dim: rgba(232, 139, 139, 0.35);
  --warn: #e3b341;
  --ok: #8fcb9b;
  --user-bubble: #26262a;
  --radius: 16px;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "SF Pro Text", Inter, "Segoe UI", Roboto,
    system-ui, Arial, sans-serif;
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overscroll-behavior: none;
}
::selection { background: rgba(255, 255, 255, 0.18); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.error { color: var(--danger); min-height: 1.2em; margin: 0.3em 0; font-size: 0.9rem; }
.ok-msg { color: var(--ok); min-height: 1.2em; margin: 0.3em 0; font-size: 0.9rem; }
button {
  background: var(--panel2); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 0.55em 1.1em; cursor: pointer; font: inherit;
  transition: background 0.15s, border-color 0.15s, opacity 0.15s;
}
button:hover { background: var(--panel3); }
button.primary {
  background: var(--accent); color: var(--on-accent);
  border: 1px solid var(--accent); font-weight: 600;
}
button.primary:hover { background: #e4e4e7; border-color: #e4e4e7; }
button.big { padding: 0.75em 1.2em; font-size: 1rem; border-radius: 12px; }
button.danger { background: transparent; color: var(--danger); border-color: var(--danger-dim); }
button.danger:hover { background: rgba(232, 139, 139, 0.08); }
button.link { background: none; border: none; color: var(--muted); }
button.link:hover { background: none; color: var(--text); }
button:disabled { opacity: 0.45; cursor: default; }
button:disabled:hover { background: var(--panel2); }
button.primary:disabled:hover { background: var(--accent); }
.small-btn { padding: 0.38em 0.9em; font-size: 0.88rem; border-radius: 999px; }
.icon-btn {
  border: none; background: none; color: var(--muted);
  padding: 0.45em; border-radius: 8px; position: relative;
  display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn:hover { background: var(--panel2); color: var(--text); }
.icon-btn svg { width: 20px; height: 20px; display: block; }
.badge {
  position: absolute; top: -2px; inset-inline-start: -2px;
  background: var(--accent); color: var(--on-accent);
  font-size: 0.66rem; font-weight: 700;
  min-width: 17px; height: 17px; border-radius: 999px;
  display: inline-flex; align-items: center; justify-content: center; padding: 0 5px;
}
:focus-visible { outline: 2px solid rgba(255, 255, 255, 0.55); outline-offset: 2px; }
input, textarea, select {
  background: var(--panel); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 0.65em 0.8em; font: inherit; width: 100%;
  transition: border-color 0.15s;
}
input:focus, textarea:focus, select:focus { border-color: var(--border-strong); outline: none; }
input::placeholder, textarea::placeholder { color: #6d6d74; }
label { display: block; margin-bottom: 0.9em; font-size: 0.95rem; }
label input, label textarea, label select { margin-top: 0.35em; }
.switch-row { display: flex; align-items: center; gap: 0.6em; cursor: pointer; }
.switch-row input[type="checkbox"] { width: 18px; height: 18px; accent-color: #d4d4d8; flex-shrink: 0; }

/* ---------- Auth ---------- */
#auth-view {
  display: flex; align-items: center; justify-content: center;
  min-height: 100dvh; padding: 1rem; background: var(--bg);
}
.auth-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 16px;
  padding: 2.2rem 2rem; width: 100%; max-width: 380px; text-align: center;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
}
.logo-mark {
  width: 54px; height: 54px; margin: 0 auto 0.7rem;
  display: flex; align-items: center; justify-content: center;
  background: var(--panel2); border: 1px solid var(--border); border-radius: 14px;
  color: var(--text); font-size: 1.5rem; font-weight: 600;
}
.auth-card h1 { margin: 0.2em 0 0.1em; font-size: 1.45rem; font-weight: 600; letter-spacing: -0.01em; }
.auth-card form { text-align: start; margin-top: 1.2rem; }
.auth-card button[type="submit"] { width: 100%; }

/* ---------- App shell ---------- */
#app-view { display: flex; flex-direction: column; height: 100dvh; }
.topbar {
  display: flex; align-items: center; gap: 0.25rem;
  padding: calc(0.45rem + var(--sat)) 0.8rem 0.45rem;
  background: rgba(0, 0, 0, 0.82);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border); z-index: 30; flex-shrink: 0;
}
.logo { font-weight: 600; font-size: 0.98rem; letter-spacing: -0.01em; }
.spacer { flex: 1; }
.layout { display: flex; flex: 1; min-height: 0; position: relative; }

/* Sidebar */
#sidebar {
  width: 280px; flex-shrink: 0; background: var(--bg-soft);
  border-inline-end: 1px solid var(--border); padding: 0.9rem;
  overflow-y: auto;
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; }
.sidebar-nav-btn { width: 100%; margin-top: 0.9rem; border-radius: 10px; background: var(--panel); }
.sidebar-head h2 { font-size: 0.82rem; margin: 0.3em 0; color: var(--muted); font-weight: 500; }
.bot-list { list-style: none; margin: 0.6rem 0; padding: 0; display: flex; flex-direction: column; gap: 0.2rem; }
.bot-list li {
  display: flex; align-items: center; gap: 0.7rem;
  padding: 0.55em 0.6em; border-radius: 10px; cursor: pointer;
  border: 1px solid transparent; transition: background 0.15s;
}
.bot-list li:hover { background: var(--panel2); }
.bot-list li.active { background: var(--panel2); border-color: var(--border); }
.avatar {
  width: 38px; height: 38px; border-radius: 10px; flex-shrink: 0;
  display: inline-flex; align-items: center; justify-content: center;
  font-weight: 600; font-size: 1rem; color: var(--text);
  background: var(--panel3); border: 1px solid var(--border);
}
.avatar.small { width: 26px; height: 26px; border-radius: 8px; font-size: 0.8rem; }
.bot-meta { min-width: 0; flex: 1; }
.bot-meta .bot-name { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bot-meta .bot-preview {
  color: var(--muted); font-size: 0.8rem; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; direction: ltr; text-align: end;
}

/* Main panel */
#main-panel { flex: 1; padding: 0.9rem 1rem calc(0.9rem + var(--sab)); min-width: 0; display: flex; flex-direction: column; }
#bot-panel {
  flex: 1; min-height: 0; width: 100%; max-width: 780px; margin-inline: auto;
  display: flex; flex-direction: column;
}
.empty { color: var(--muted); text-align: center; margin-top: 2.5rem; }
.empty.hero .hero-mark {
  width: 60px; height: 60px; margin: 0 auto 0.8rem;
  display: flex; align-items: center; justify-content: center;
  background: var(--panel2); border: 1px solid var(--border); border-radius: 16px;
  color: var(--text); font-size: 1.6rem; font-weight: 600;
}
.empty.hero h3 { color: var(--text); margin: 0.3em 0; font-weight: 600; }
.bot-head { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }
.bot-title { display: flex; align-items: center; gap: 0.6rem; }
.bot-head h2 { margin: 0.2em 0; font-size: 1.12rem; font-weight: 600; letter-spacing: -0.01em; }
.segmented {
  display: inline-flex; background: var(--panel); border-radius: 999px;
  padding: 3px; gap: 2px; border: 1px solid var(--border);
}
.segmented .tab {
  border: none; background: none; border-radius: 999px; padding: 0.38em 1em;
  color: var(--muted); font-size: 0.9rem;
}
.segmented .tab:hover { background: none; color: var(--text); }
.segmented .tab.active { background: var(--panel3); color: var(--text); font-weight: 500; }
.tab-page { margin-top: 0.9rem; display: flex; flex-direction: column; flex: 1; min-height: 0; }

/* Conversations */
.conv-bar { display: flex; gap: 0.5rem; align-items: center; }
.conv-chips {
  flex: 1; display: flex; gap: 0.4rem; overflow-x: auto; padding: 0.2rem 0;
  scrollbar-width: none;
}
.conv-chips::-webkit-scrollbar { display: none; }
.chip {
  flex-shrink: 0; max-width: 200px; white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis; border-radius: 999px; padding: 0.38em 0.9em;
  background: var(--panel); color: var(--muted); font-size: 0.87rem;
  display: inline-flex; align-items: center;
}
.chip.active { background: var(--panel3); color: var(--text); border-color: var(--border-strong); font-weight: 500; }
.chip-tag {
  font-size: 0.66rem; font-weight: 600; color: var(--muted);
  border: 1px solid var(--border); border-radius: 999px;
  padding: 0.1em 0.5em; margin-inline-end: 0.5em; flex-shrink: 0;
}

/* Messages */
.messages {
  flex: 1; overflow-y: auto; padding: 1rem 0.1rem;
  display: flex; flex-direction: column; gap: 1.1rem; min-height: 200px;
  scroll-behavior: smooth;
}
.msg-row { display: flex; align-items: flex-end; gap: 0.5rem; max-width: 88%; }
.msg-row.user { align-self: flex-end; }
.msg-row.assistant { align-self: flex-start; align-items: flex-start; max-width: 100%; }
.msg-row.assistant .avatar { margin-top: 0.15rem; }
.msg-col { display: flex; flex-direction: column; min-width: 0; }
.msg-row.assistant .msg-col { flex: 1; }
.msg {
  padding: 0.65em 0.95em; border-radius: var(--radius);
  white-space: pre-wrap; word-break: break-word; line-height: 1.55; font-size: 0.98rem;
}
.msg.user {
  background: var(--user-bubble); color: var(--text);
  border-radius: 20px;
}
.msg.assistant {
  background: transparent; border: none;
  padding: 0.15em 0.1em; border-radius: 0;
}
.msg-time { color: var(--muted); font-size: 0.7rem; margin-top: 0.25em; padding: 0 0.4em; }
.msg-row.assistant .msg-time { text-align: end; padding: 0 0.1em; }
.msg.stopped { opacity: 0.7; font-style: italic; }
/* typing indicator */
.typing { display: inline-flex; gap: 5px; padding: 0.6em 0.1em; }
.typing span {
  width: 7px; height: 7px; border-radius: 50%; background: var(--muted);
  animation: blink 1.2s infinite;
}
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 0.9; } }

/* Tool trace */
.trace {
  margin-top: 0.5em; border: 1px solid var(--border); border-radius: 12px;
  background: rgba(255, 255, 255, 0.02); font-size: 0.82rem; overflow: hidden;
}
.trace > summary {
  cursor: pointer; padding: 0.5em 0.8em; color: var(--muted);
  list-style: none; user-select: none;
  display: flex; align-items: center; justify-content: space-between; gap: 0.5em;
}
.trace > summary::-webkit-details-marker { display: none; }
.trace > summary::after {
  content: "›"; font-size: 1.05rem; line-height: 1;
  transition: transform 0.15s;
}
.trace[open ] > summary::after { transform: rotate(90deg); }
.trace-items { padding: 0 0.8em 0.6em; display: flex; flex-direction: column; gap: 0.45em; }
.trace-item { display: flex; gap: 0.5em; align-items: flex-start; }
.trace-item .t-icon { flex-shrink: 0; color: var(--muted); font-size: 0.85em; line-height: 1.5; }
.trace-item .t-icon.st-ok, .trace-item .t-icon.st-approved { color: var(--ok); }
.trace-item .t-icon.st-error, .trace-item .t-icon.st-denied { color: var(--danger); }
.trace-item .t-icon.st-timeout { color: var(--warn); }
.trace-item .t-icon.st-running { animation: blink 1.2s infinite; }
.trace-item .t-name { font-family: ui-monospace, "SF Mono", Menlo, monospace; direction: ltr; font-weight: 600; }
.trace-item .t-preview {
  color: var(--muted); white-space: pre-wrap; word-break: break-word;
  max-height: 5.2em; overflow: hidden; flex: 1;
}

/* Inline approval card */
.approval-card {
  margin-top: 0.5em; border: 1px solid var(--border);
  border-inline-start: 3px solid var(--warn);
  background: var(--panel); border-radius: 12px; padding: 0.8em;
}
.approval-card h4 { margin: 0 0 0.4em; font-size: 0.92rem; font-weight: 600; }
.approval-card pre, .card pre {
  direction: ltr; text-align: left; background: var(--code);
  border: 1px solid var(--border);
  padding: 0.6em; border-radius: 10px; overflow-x: auto; font-size: 0.82rem;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  margin: 0.4em 0; white-space: pre-wrap; word-break: break-word;
}
.approval-card .row { margin-top: 0.5em; }
.approval-card .decided { color: var(--muted); font-size: 0.88rem; }

/* Composer */
.composer {
  display: flex; gap: 0.5rem; align-items: flex-end;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 24px; padding: 0.4rem;
  margin-top: 0.4rem; transition: border-color 0.15s;
}
.composer:focus-within { border-color: var(--border-strong); }
.composer textarea {
  flex: 1; resize: none; border: none; background: none;
  padding: 0.5em 0.6em; font-size: 1rem;
  max-height: 130px; line-height: 1.45;
}
.composer textarea:focus { border: none; }
.send-btn {
  width: 36px; height: 36px; border-radius: 50%; flex-shrink: 0;
  background: var(--accent); color: var(--on-accent); border: none;
  font-size: 1.05rem; font-weight: 700; line-height: 1;
  display: inline-flex; align-items: center; justify-content: center; padding: 0;
}
.send-btn:hover { background: #e4e4e7; }
.send-btn.stop { background: var(--panel3); color: var(--text); font-size: 0.7rem; }
.send-btn.stop:hover { background: #333338; }

/* Memory / generic lists */
.memory-list { list-style: none; padding: 0; margin: 0 0 1rem; }
.memory-list li {
  display: flex; gap: 0.5rem; align-items: center;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 12px; padding: 0.6em 0.8em; margin-bottom: 0.5rem;
}
.memory-list .note-text { flex: 1; }
.inline-form { display: flex; gap: 0.5rem; }
.inline-form input { flex: 1; }
.card-list { display: flex; flex-direction: column; gap: 0.6rem; margin-bottom: 1rem; }
.card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
  padding: 0.9em 1em;
}
.card h4 { margin: 0 0 0.3em; font-weight: 600; }
.card .meta { color: var(--muted); font-size: 0.83rem; }
.card .row { margin-top: 0.6em; }
.status-pill {
  display: inline-block; padding: 0.15em 0.7em; border-radius: 999px;
  font-size: 0.78rem; font-weight: 500;
}
.status-pill.pending { background: rgba(227, 179, 65, 0.12); color: var(--warn); }
.status-pill.approved { background: rgba(143, 203, 155, 0.12); color: var(--ok); }
.status-pill.denied, .status-pill.timeout { background: rgba(232, 139, 139, 0.12); color: var(--danger); }

/* Forms */
.stack-form { max-width: 560px; }
.stack-form.wide { max-width: 640px; }
.row { display: flex; gap: 0.6rem; flex-wrap: wrap; }
.card-form {
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
  padding: 1rem; margin-top: 0.5rem;
}
.card-form h3 { margin-top: 0; font-weight: 600; }
.settings-section {
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
  padding: 1.1rem; margin-bottom: 1rem;
}
.settings-section h3 { margin: 0 0 0.9em; font-size: 0.98rem; font-weight: 600; }
.section-title { margin: 1.2em 0 0.6em; font-weight: 600; }

/* Bot library */
.filter-chips { display: flex; gap: 0.4rem; flex-wrap: wrap; margin-bottom: 1rem; }
.library-grid {
  display: grid; gap: 0.7rem;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
}
.lib-card { display: flex; flex-direction: column; margin-bottom: 0; }
.lib-card p { color: var(--muted); font-size: 0.9rem; line-height: 1.5; margin: 0 0 0.6em; flex: 1; }
.lib-card .meta a { color: var(--muted); text-decoration: underline; }
.lib-card .meta a:hover { color: var(--text); }
.lib-card .row { margin-top: 0.7em; }

/* Subviews (settings / approvals) */
.subview {
  position: absolute; inset: 0; top: 0; background: var(--bg); z-index: 20;
  display: flex; flex-direction: column;
  padding-top: calc(0.2rem + var(--sat));
}
#app-view > .subview { position: fixed; }
.subview-head {
  display: flex; align-items: center; gap: 0.5rem;
  padding: 0.7rem 1rem; border-bottom: 1px solid var(--border);
}
.subview-head h2 { margin: 0; font-size: 1.05rem; font-weight: 600; }
.back-btn { font-size: 1.6rem; line-height: 1; padding: 0.1em 0.35em; color: var(--muted); }
.subview-body { flex: 1; overflow-y: auto; padding: 1rem; padding-bottom: calc(1rem + var(--sab)); }

/* Scrollbars */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: var(--panel3); border-radius: 8px; }

/* Mobile */
#menu-btn { display: none; }
@media (max-width: 760px) {
  #menu-btn { display: inline-flex; }
  #sidebar {
    position: fixed; top: 0; bottom: 0; inset-inline-start: 0; z-index: 40;
    transform: translateX(0); transition: transform 0.25s ease;
    width: 82vw; max-width: 320px; padding-top: calc(0.9rem + var(--sat));
    box-shadow: 0 0 40px rgba(0,0,0,0.6); background: var(--bg-soft);
  }
  [dir="rtl"] #sidebar.closed { transform: translateX(105%); }
  [dir="ltr"] #sidebar.closed { transform: translateX(-105%); }
  .msg-row { max-width: 94%; }
  .msg-row.assistant { max-width: 100%; }
  #user-email { display: none; }
  .bot-head { gap: 0.6rem; }
}
