:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #fbfbfc;
  --border: #e6e8eb;
  --border-strong: #d4d7dc;
  --text: #1a1d21;
  --text-soft: #5b6470;
  --text-faint: #8b929c;
  --accent: #0a66c2;
  --accent-hover: #084e95;
  --accent-soft: #e8f1fb;
  --good: #1f8a4c;
  --good-soft: #e7f4ec;
  --bad: #c0392b;
  --bad-soft: #fbecea;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 1px 3px rgba(16, 24, 40, .06);
  --shadow-lg: 0 4px 6px rgba(16, 24, 40, .04), 0 12px 24px rgba(16, 24, 40, .08);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
button { font-family: inherit; cursor: pointer; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.hidden { display: none !important; }

/* ---------- Layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 16px;
  height: 60px; padding: 0 24px;
  background: rgba(255, 255, 255, .85);
  backdrop-filter: saturate(160%) blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; letter-spacing: -.01em; }
.brand .logo {
  width: 30px; height: 30px; border-radius: 7px;
  background: var(--accent); color: #fff;
  display: grid; place-items: center;
}
.topbar .spacer { flex: 1; }
.topbar .meta { color: var(--text-soft); font-size: 13px; }

.shell { max-width: 1080px; margin: 0 auto; padding: 28px 24px 80px; }
.grid { display: grid; gap: 20px; }

/* ---------- Cards ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.card-pad { padding: 20px 22px; }
.card-head {
  display: flex; align-items: center; gap: 10px;
  padding: 16px 22px; border-bottom: 1px solid var(--border);
}
.card-head h2 { margin: 0; font-size: 15px; font-weight: 650; }
.card-head .sub { color: var(--text-faint); font-size: 13px; font-weight: 400; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 7px;
  height: 38px; padding: 0 16px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text);
  font-size: 14px; font-weight: 550;
  transition: background .12s, border-color .12s, transform .04s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(.5px); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn svg { width: 16px; height: 16px; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-ghost { border-color: transparent; background: transparent; color: var(--text-soft); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-sm { height: 32px; padding: 0 12px; font-size: 13px; }
.btn-icon { width: 38px; padding: 0; justify-content: center; }

/* ---------- Forms ---------- */
input[type="text"], input[type="password"], textarea {
  width: 100%; padding: 10px 12px;
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  font-family: inherit; font-size: 14px; color: var(--text);
  background: var(--surface); transition: border-color .12s, box-shadow .12s;
}
input:focus, textarea:focus {
  outline: none; border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
textarea { resize: vertical; min-height: 90px; line-height: 1.5; }
label.field-label { display: block; font-size: 13px; font-weight: 550; color: var(--text-soft); margin-bottom: 6px; }

/* ---------- Tabs ---------- */
.tabs { display: flex; gap: 4px; margin-bottom: 18px; }
.tab {
  height: 36px; padding: 0 16px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface);
  color: var(--text-soft); font-size: 13.5px; font-weight: 550;
}
.tab.active { background: var(--text); border-color: var(--text); color: #fff; }
.tab .count {
  margin-left: 6px; padding: 1px 7px; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent); font-size: 12px;
}
.tab.active .count { background: rgba(255,255,255,.2); color: #fff; }

/* ---------- Chips (queries) ---------- */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  display: inline-flex; align-items: center; gap: 8px;
  height: 32px; padding: 0 6px 0 12px;
  border: 1px solid var(--border-strong); border-radius: 999px;
  background: var(--surface-2); font-size: 13px;
}
.chip.suggested { border-style: dashed; }
.chip .x {
  width: 20px; height: 20px; border-radius: 999px; border: none;
  background: transparent; color: var(--text-faint);
  display: grid; place-items: center;
}
.chip .x:hover { background: var(--bad-soft); color: var(--bad); }

/* ---------- Post cards ---------- */
.post { padding: 20px 22px; }
.post + .post { border-top: 1px solid var(--border); }
.post-head { display: flex; align-items: flex-start; gap: 12px; }
.avatar {
  width: 42px; height: 42px; border-radius: 50%; flex: none;
  background: var(--accent-soft); color: var(--accent);
  display: grid; place-items: center; font-weight: 650; font-size: 15px;
}
.post-author { font-weight: 600; line-height: 1.3; }
.post-byline { color: var(--text-faint); font-size: 13px; }
.post-tag {
  margin-left: auto; flex: none;
  padding: 3px 10px; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent);
  font-size: 12px; font-weight: 550;
}
.post-body {
  margin: 12px 0 0; color: var(--text);
  white-space: pre-wrap; max-height: 220px; overflow: hidden;
  position: relative;
}
.post-body.clamped::after {
  content: ""; position: absolute; left: 0; right: 0; bottom: 0; height: 48px;
  background: linear-gradient(transparent, var(--surface));
}
.post-actions { display: flex; align-items: center; gap: 8px; margin-top: 16px; flex-wrap: wrap; }
.vote {
  display: inline-flex; align-items: center; gap: 7px;
  height: 36px; padding: 0 14px; border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong); background: var(--surface);
  font-size: 13.5px; font-weight: 550; color: var(--text-soft);
}
.vote svg { width: 16px; height: 16px; }
.vote.up:hover { border-color: var(--good); color: var(--good); background: var(--good-soft); }
.vote.down:hover { border-color: var(--bad); color: var(--bad); background: var(--bad-soft); }
.vote.up.on { border-color: var(--good); color: var(--good); background: var(--good-soft); }
.vote.down.on { border-color: var(--bad); color: var(--bad); background: var(--bad-soft); }

/* ---------- Draft panel ---------- */
.draft { margin-top: 14px; padding: 16px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface-2); }
.draft-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; font-size: 13px; font-weight: 600; color: var(--text-soft); }
.draft-actions { display: flex; gap: 8px; margin-top: 12px; }

/* ---------- Empty / states ---------- */
.empty { text-align: center; padding: 56px 24px; color: var(--text-faint); }
.empty svg { width: 40px; height: 40px; opacity: .4; margin-bottom: 12px; }

/* ---------- Login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.login-card { width: 100%; max-width: 380px; }
.login-card .brand { justify-content: center; margin-bottom: 6px; }
.login-card p { text-align: center; color: var(--text-faint); font-size: 13.5px; margin: 0 0 20px; }

/* ---------- Toast ---------- */
.toast-wrap { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); z-index: 50; display: flex; flex-direction: column; gap: 8px; }
.toast {
  padding: 11px 16px; border-radius: var(--radius-sm);
  background: var(--text); color: #fff; font-size: 13.5px;
  box-shadow: var(--shadow-lg); animation: rise .18s ease;
}
.toast.err { background: var(--bad); }
.toast.ok { background: var(--good); }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } }

/* ---------- Spinner ---------- */
.spin { width: 15px; height: 15px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Live log ---------- */
.logbox {
  margin-bottom: 20px; overflow: hidden;
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface);
  box-shadow: var(--shadow);
}
.logbox-head {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 18px; border-bottom: 1px solid var(--border);
}
.logbox-head .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-faint); }
.logbox-head .dot.live { background: var(--accent); animation: pulse 1.2s ease-in-out infinite; }
.logbox-head .dot.done { background: var(--good); }
.logbox-head .dot.err { background: var(--bad); }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .3; } }
.logbox-head .title { font-size: 13.5px; font-weight: 600; }
.logbox-head .stat { color: var(--text-faint); font-size: 12.5px; }
.log-lines {
  max-height: 280px; overflow-y: auto; padding: 12px 18px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12.5px; line-height: 1.65; background: var(--surface-2);
}
.log-line { display: flex; gap: 10px; white-space: pre-wrap; word-break: break-word; }
.log-line .t { color: var(--text-faint); flex: none; }
.log-line.info .m { color: var(--text); }
.log-line.debug .m { color: var(--text-soft); }
.log-line.warn .m { color: #9a6b00; }
.log-line.error .m { color: var(--bad); }

/* ---------- Query inline edit ---------- */
.chip .pencil {
  width: 20px; height: 20px; border-radius: 999px; border: none;
  background: transparent; color: var(--text-faint);
  display: grid; place-items: center;
}
.chip .pencil:hover { background: var(--accent-soft); color: var(--accent); }
.chip input.chip-edit {
  width: 140px; height: 22px; padding: 0 6px; font-size: 13px;
  border: 1px solid var(--accent); border-radius: 6px; box-shadow: none;
}

/* ---------- Modal ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(16, 24, 40, .45);
  display: grid; place-items: center; padding: 24px;
  animation: fade .15s ease;
}
@keyframes fade { from { opacity: 0; } }
.modal {
  width: 100%; max-width: 520px; max-height: 90vh; overflow: auto;
  box-shadow: var(--shadow-lg); animation: pop .16s ease;
}
@keyframes pop { from { transform: translateY(10px) scale(.99); opacity: .6; } }

.segmented { display: inline-flex; padding: 3px; gap: 3px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; }
.seg { height: 34px; padding: 0 16px; border: none; border-radius: 7px; background: transparent; color: var(--text-soft); font-size: 13.5px; font-weight: 550; }
.seg.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.set-flag { font-size: 12px; font-weight: 500; color: var(--good); }
.set-flag:empty { display: none; }
.hint { font-size: 12.5px; color: var(--text-faint); line-height: 1.55; margin: 8px 0 0; }
.hint strong { color: var(--text-soft); }
details summary { padding: 4px 0; }

/* ---------- Session status ---------- */
.session-status { display: flex; align-items: center; gap: 10px; font-size: 14px; font-weight: 550; }
.session-status .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--text-faint); flex: none; }
.session-status .dot.ok { background: var(--good); }
.session-status .dot.off { background: var(--bad); }
.cmd-box {
  display: flex; align-items: center; gap: 10px;
  background: #0f1419; border-radius: var(--radius-sm); padding: 12px 14px;
}
.cmd-box code {
  flex: 1; color: #d7e0ea; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px; overflow-x: auto; white-space: nowrap;
}
.cmd-box .btn { background: rgba(255,255,255,.08); border-color: rgba(255,255,255,.14); color: #e6ebf1; flex: none; }
.cmd-box .btn:hover { background: rgba(255,255,255,.16); }

/* ---------- Signal chips (toggle) ---------- */
.chip.off { opacity: .45; }
.chip.toggle { cursor: pointer; }

@media (max-width: 640px) {
  .shell { padding: 18px 14px 60px; }
  .post, .card-pad, .card-head { padding-left: 16px; padding-right: 16px; }
}
