:root {
  --canvas: #f3f4f6;
  --panel: #ffffff;
  --panel-2: #f7f8fa;
  --border: #e1e4e8;
  --border-strong: #c9ced4;
  --text: #1b1f24;
  --muted: #5f6a76;
  --accent: #0f6cbd;
  --accent-hover: #0b5aa0;
  --accent-soft: #e8f1fb;
  --green: #107c41;
  --green-soft: #e6f2ea;
  --amber: #9a6700;
  --amber-soft: #fdf3d7;
  --red: #c0392b;
  --red-soft: #fdecea;
  --radius: 8px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Segoe UI", -apple-system, BlinkMacSystemFont, "Helvetica Neue", Arial, sans-serif;
  background: var(--canvas);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
}

.layout { display: flex; min-height: 100vh; }

.sidebar {
  width: 232px;
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
  padding: 18px 12px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  position: sticky;
  top: 0;
  height: 100vh;
}

.logo {
  font-size: 17px;
  font-weight: 600;
  padding: 0 10px;
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
}

.icon {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex-shrink: 0;
}

.logo-icon {
  width: 22px;
  height: 22px;
  flex-shrink: 0;
  border-radius: 5px;
  box-shadow: 0 1px 2px rgba(15, 108, 189, 0.25);
}

.auth-logo .logo-icon { width: 30px; height: 30px; border-radius: 7px; }

nav { display: flex; flex-direction: column; gap: 2px; }

.nav-btn {
  display: flex;
  align-items: center;
  gap: 9px;
  background: none;
  border: none;
  color: var(--text);
  font-family: inherit;
  font-size: 13.5px;
  text-align: left;
  padding: 8px 10px;
  border-radius: 5px;
  cursor: pointer;
}
.nav-btn:hover { background: var(--panel-2); }
.nav-btn.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.pill {
  margin-left: auto;
  background: #dde2e8;
  color: #3d4753;
  border-radius: 999px;
  font-size: 11px;
  padding: 0 7px;
  font-weight: 600;
  line-height: 18px;
}
.nav-btn.active .pill { background: var(--accent); color: #fff; }
.pill:empty { display: none; }

.sidebar-foot { margin-top: auto; }

.content { flex: 1; padding: 24px 32px; max-width: 1060px; }

.tab { display: none; }
.tab.active { display: block; }

.tab-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  gap: 12px;
  flex-wrap: wrap;
}
.tab-head h1 { font-size: 20px; font-weight: 600; margin: 0; }

button.primary, button.ghost, button.small {
  border-radius: 5px;
  cursor: pointer;
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  padding: 7px 14px;
  border: 1px solid transparent;
}
button.primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}
button.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
button.ghost {
  background: var(--panel);
  border: 1px solid var(--border-strong);
  color: var(--text);
}
button.ghost:hover { background: var(--panel-2); }
button.small { padding: 4px 10px; font-size: 12.5px; }
button:disabled { opacity: 0.55; cursor: wait; }

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 15px 18px;
  margin-bottom: 10px;
  box-shadow: 0 1px 2px rgba(27, 31, 36, 0.04);
}
.card.empty { color: var(--muted); }

.card-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
}
.card h3 { margin: 0 0 3px; font-size: 15px; font-weight: 600; }
.meta { color: var(--muted); font-size: 12.5px; margin-bottom: 6px; }
.summary { margin: 6px 0; }
.suggested { color: var(--accent); font-size: 13px; margin-top: 4px; }

.badges { display: flex; gap: 6px; flex-wrap: wrap; margin: 6px 0; }
.badge {
  font-size: 11px;
  font-weight: 600;
  padding: 1px 8px;
  border-radius: 3px;
  letter-spacing: 0.2px;
}
.badge.high { background: var(--red-soft); color: var(--red); }
.badge.normal { background: var(--amber-soft); color: var(--amber); }
.badge.low { background: #eceff2; color: var(--muted); }
.badge.cat { background: var(--accent-soft); color: var(--accent); }
.badge.reply { background: #ede9fb; color: #5b4ba8; }
.badge.status-processed, .badge.status-done, .badge.status-sent, .badge.status-approved { background: var(--green-soft); color: var(--green); }
.badge.status-new, .badge.status-pending, .badge.status-open { background: var(--amber-soft); color: var(--amber); }
.badge.status-triaged, .badge.status-in_progress { background: var(--accent-soft); color: var(--accent); }

.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }

details.raw { margin-top: 8px; }
details.raw summary { color: var(--muted); font-size: 12.5px; cursor: pointer; }
details.raw pre, .email-body {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 12px;
  white-space: pre-wrap;
  font-size: 13px;
  font-family: inherit;
  color: var(--text);
  overflow-x: auto;
}

/* Markdown body */
.md-body h1 { font-size: 19px; font-weight: 600; border-bottom: 1px solid var(--border); padding-bottom: 8px; }
.md-body h2 { font-size: 15.5px; font-weight: 600; margin-top: 20px; }
.md-body h3 { font-size: 14px; font-weight: 600; margin-top: 14px; }
.md-body ul { padding-left: 22px; }
.md-body li { margin: 3px 0; }
.md-body table { border-collapse: collapse; width: 100%; margin: 10px 0; }
.md-body th, .md-body td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; font-size: 13px; }
.md-body th { background: var(--panel-2); }

/* Modal */
dialog {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px;
  width: min(640px, 92vw);
  max-height: 86vh;
  overflow-y: auto;
  box-shadow: 0 8px 30px rgba(27, 31, 36, 0.12);
}
dialog::backdrop { background: rgba(27, 31, 36, 0.35); }
dialog h2 { margin-top: 0; font-size: 16px; font-weight: 600; }
dialog label { display: block; font-size: 12.5px; color: var(--muted); margin: 12px 0 4px; }
dialog input, dialog textarea, dialog select {
  width: 100%;
  background: var(--panel);
  border: 1px solid var(--border-strong);
  color: var(--text);
  border-radius: 5px;
  padding: 8px 11px;
  font-size: 13.5px;
  font-family: inherit;
}
dialog input:focus, dialog textarea:focus, dialog select:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  border-color: var(--accent);
}
dialog textarea { min-height: 140px; resize: vertical; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }

/* Toast */
#toast {
  position: fixed;
  bottom: 22px;
  right: 22px;
  background: #2b3036;
  color: #fff;
  padding: 10px 16px;
  border-radius: 6px;
  font-size: 13px;
  opacity: 0;
  transform: translateY(8px);
  transition: all .25s;
  pointer-events: none;
  z-index: 50;
  box-shadow: 0 4px 14px rgba(27, 31, 36, 0.25);
}
#toast.show { opacity: 1; transform: none; }
#toast.error { background: var(--red); }

.spinner-note { color: var(--muted); font-size: 13px; display: inline-flex; align-items: center; gap: 8px; }
.spinner {
  width: 13px; height: 13px;
  border: 2px solid rgba(255, 255, 255, 0.4);
  border-top-color: currentColor;
  border-radius: 50%;
  animation: spin .8s linear infinite;
  display: inline-block;
}
button.ghost .spinner, button.small.ghost .spinner {
  border-color: var(--border-strong);
  border-top-color: var(--accent);
}
@keyframes spin { to { transform: rotate(360deg); } }

.conn-status { font-weight: 600; font-size: 12.5px; }
.conn-status.on { color: var(--green); }
.conn-status.off { color: var(--amber); }
code {
  background: var(--panel-2);
  border: 1px solid var(--border);
  padding: 0 5px;
  border-radius: 4px;
  font-size: 12px;
  font-family: Consolas, "Cascadia Mono", Menlo, monospace;
}

/* ---------- usage tab ---------- */
.stat-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 10px;
  margin-bottom: 12px;
}
.stat { text-align: center; padding: 16px 12px; }
.stat-num { font-size: 24px; font-weight: 600; margin-bottom: 2px; }
.usage-table { width: 100%; border-collapse: collapse; margin-top: 8px; }
.usage-table th, .usage-table td {
  text-align: left;
  padding: 7px 10px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.usage-table th { color: var(--muted); font-weight: 600; }
.usage-table tr:last-child td { border-bottom: none; }

/* ---------- auth ---------- */
.auth-screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--canvas);
  z-index: 100;
}
.auth-screen[hidden] { display: none; }
.auth-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 30px 32px;
  width: min(380px, 92vw);
  box-shadow: 0 2px 10px rgba(27, 31, 36, 0.06);
}
.auth-card h2 { text-align: center; font-size: 17px; font-weight: 600; margin: 4px 0 10px; }
.auth-card label { display: block; font-size: 12.5px; color: var(--muted); margin: 12px 0 4px; }
.auth-card input {
  width: 100%;
  background: var(--panel);
  border: 1px solid var(--border-strong);
  color: var(--text);
  border-radius: 5px;
  padding: 8px 11px;
  font-size: 13.5px;
  font-family: inherit;
}
.auth-card input:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  border-color: var(--accent);
}
.auth-card button.primary { width: 100%; margin-top: 18px; padding: 9px; }
.auth-switch { text-align: center; margin: 14px 0 0; }
.auth-switch a { color: var(--accent); text-decoration: none; }
.auth-switch a:hover { text-decoration: underline; }
.auth-error { color: var(--red); font-size: 13px; text-align: center; min-height: 18px; margin: 10px 0 0; }

.user-chip {
  display: flex;
  align-items: center;
  gap: 9px;
  margin-top: 10px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel-2);
}
.user-chip[hidden] { display: none; }
.avatar {
  width: 30px; height: 30px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.user-meta { flex: 1; min-width: 0; }
.user-name { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-role { font-size: 11px; color: var(--muted); }
#logout-btn { padding: 5px 7px; }

/* ---------- global search ---------- */
#global-search {
  width: 100%;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 7px 11px;
  font-size: 13px;
  font-family: inherit;
}
#global-search:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* ---------- follow-up radar ---------- */
.fu-group h3 { font-size: 13px; color: var(--muted); margin: 12px 0 4px; font-weight: 600; }
.fu-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 7px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13.5px;
}
.fu-item:last-child { border-bottom: none; }
.meta-inline { color: var(--muted); font-size: 12px; }

/* ---------- search results ---------- */
.search-hit {
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  margin-bottom: 6px;
  cursor: pointer;
  font-size: 13.5px;
}
.search-hit:hover { background: var(--accent-soft); border-color: var(--accent); }

/* ---------- mobile ---------- */
@media (max-width: 760px) {
  .layout { flex-direction: column; }
  .sidebar {
    width: 100%;
    height: auto;
    position: static;
    flex-direction: column;
    padding: 12px;
    gap: 10px;
  }
  nav { flex-direction: row; flex-wrap: wrap; gap: 4px; }
  .nav-btn { padding: 7px 9px; font-size: 13px; }
  .sidebar-foot { margin-top: 0; display: flex; align-items: center; gap: 10px; }
  .sidebar-foot .user-chip { margin-top: 0; flex: 1; }
  .content { padding: 16px 14px; }
  .tab-head { flex-direction: column; align-items: flex-start; }
  .chat-msg { max-width: 95%; }
  .stat-row { grid-template-columns: 1fr 1fr; }
}

/* ---------- autopilot ---------- */
#autopilot-toggle.on { border-color: var(--green); color: var(--green); font-weight: 600; }
.badge.status-processing { background: var(--accent-soft); color: var(--accent); }

/* ---------- ask workpilot (ChatGPT-style) ---------- */
.ask-layout {
  display: flex;
  height: calc(100vh - 48px);
  margin: -24px -32px;
  background: var(--panel);
}
.ask-sidebar {
  width: 240px;
  flex-shrink: 0;
  background: var(--panel-2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 12px;
  gap: 6px;
}
.ask-new {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  background: var(--panel);
  border: 1px solid var(--border-strong);
  color: var(--text);
  border-radius: 8px;
  padding: 9px 12px;
  font-size: 13.5px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
}
.ask-new:hover { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.ask-history-label {
  font-size: 11.5px;
  font-weight: 600;
  color: var(--muted);
  padding: 10px 8px 2px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
}
.ask-history { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.ask-history-empty { color: var(--muted); font-size: 13px; padding: 8px; }
.conv-item {
  background: none;
  border: none;
  color: var(--text);
  font-family: inherit;
  font-size: 13.5px;
  text-align: left;
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
}
.conv-item:hover { background: #eceef1; }
.conv-item.active { background: #e4e7eb; font-weight: 600; }

.ask-main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.chat-log {
  flex: 1;
  overflow-y: auto;
  padding: 28px 20px 12px;
  display: flex;
  flex-direction: column;
  gap: 22px;
}
.chat-msg { width: min(100%, 720px); margin: 0 auto; display: flex; }
.chat-msg.user { justify-content: flex-end; }
.chat-msg.user .bubble {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 18px;
  border-bottom-right-radius: 6px;
  padding: 10px 16px;
  max-width: 80%;
  font-size: 14.5px;
  line-height: 1.55;
}
.chat-msg.assistant { gap: 12px; align-items: flex-start; }
.a-avatar { width: 28px; height: 28px; flex-shrink: 0; margin-top: 2px; }
.a-avatar svg { width: 100%; height: 100%; border-radius: 6px; }
.a-body { flex: 1; min-width: 0; font-size: 14.5px; line-height: 1.65; }
.a-body .md-body h1, .a-body .md-body h2, .a-body .md-body h3 { font-size: 15px; margin: 10px 0 4px; border: none; padding: 0; }
.a-body .md-body table { font-size: 13px; }

.ask-composer {
  width: min(100%, 720px);
  margin: 0 auto;
  padding: 10px 20px 18px;
  position: relative;
  flex-shrink: 0;
}
.ask-composer input {
  width: 100%;
  background: var(--panel);
  border: 1px solid var(--border-strong);
  color: var(--text);
  border-radius: 24px;
  padding: 13px 52px 13px 18px;
  font-size: 14.5px;
  font-family: inherit;
  box-shadow: 0 2px 10px rgba(27, 31, 36, 0.06);
}
.ask-composer input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.ask-send {
  position: absolute;
  right: 28px;
  top: 50%;
  transform: translateY(calc(-50% - 4px));
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--accent);
  border: none;
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.ask-send:hover { background: var(--accent-hover); }
.ask-send .icon { width: 15px; height: 15px; stroke-width: 2.2; }

.ask-empty {
  margin: auto;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 20px;
}
.ask-empty-logo { width: 46px; height: 46px; }
.ask-empty-logo svg { width: 100%; height: 100%; border-radius: 11px; box-shadow: 0 2px 8px rgba(15, 108, 189, 0.25); }
.ask-empty h2 { font-size: 21px; font-weight: 600; margin: 4px 0 8px; }
.ask-suggestions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; max-width: 560px; }
.sugg {
  background: var(--panel);
  border: 1px solid var(--border-strong);
  color: var(--muted);
  border-radius: 999px;
  padding: 8px 16px;
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
}
.sugg:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }

@media (max-width: 760px) {
  .ask-layout { flex-direction: column; height: calc(100vh - 170px); margin: -16px -14px; }
  .ask-sidebar { width: 100%; flex-direction: row; align-items: center; overflow-x: auto; padding: 8px; }
  .ask-new { width: auto; flex-shrink: 0; }
  .ask-history-label { display: none; }
  .ask-history { flex-direction: row; overflow-x: auto; overflow-y: hidden; }
  .conv-item { width: auto; max-width: 160px; flex-shrink: 0; }
}

/* ---------- webhook connector ---------- */
.hook-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 6px 0;
}
.hook-row code {
  flex: 1;
  overflow-x: auto;
  white-space: nowrap;
  padding: 5px 8px;
}

/* ---------- connector logos ---------- */
.conn-head { display: flex; align-items: center; gap: 12px; min-width: 0; }
.conn-head .meta { margin-bottom: 0; }
.conn-logo {
  width: 34px;
  height: 34px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.conn-logo svg { width: 30px; height: 30px; }

/* ---------- connectors search ---------- */
#conn-search {
  width: 100%;
  background: var(--panel);
  border: 1px solid var(--border-strong);
  color: var(--text);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 14px;
  font-family: inherit;
  margin-bottom: 12px;
}
#conn-search:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
