/* ============================================================ tokens === */
:root {
  --blue: #0000f2;
  --radius: 10px;
  --radius-sm: 6px;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, "Courier Prime", Consolas, monospace;
  --sidebar-w: 268px;
}

html[data-theme="dark"] {
  --bg: #0a0a0f;
  --panel: #101017;
  --panel-2: #16161f;
  --raised: #1c1c27;
  --border: #24242f;
  --border-soft: #1b1b25;
  --text: #e9e9f0;
  --text-dim: #a2a2b4;
  --muted: #74748a;
  --accent: #5b5bff;
  --accent-soft: rgba(91, 91, 255, 0.14);
  --accent-text: #a8a8ff;
  --user-bubble: #1d1d2b;
  --ok: #3ecf8e;
  --warn: #e6b45e;
  --err: #ff6b6b;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
}

html[data-theme="light"] {
  --bg: #f4f4f7;
  --panel: #ffffff;
  --panel-2: #fafafc;
  --raised: #f0f0f5;
  --border: #e2e2ea;
  --border-soft: #ececf2;
  --text: #16161d;
  --text-dim: #4d4d61;
  --muted: #7a7a8f;
  --accent: var(--blue);
  --accent-soft: rgba(0, 0, 242, 0.08);
  --accent-text: #0000c4;
  --user-bubble: #ececf5;
  --ok: #17915d;
  --warn: #a4700c;
  --err: #cc2f2f;
  --shadow: 0 8px 30px rgba(20, 20, 45, 0.12);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

body { overflow: hidden; }
.mono { font-family: var(--mono); }
.tiny { font-size: 11px; }
.muted { color: var(--muted); }
[hidden] { display: none !important; }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 6px; border: 3px solid var(--bg); }
::-webkit-scrollbar-thumb:hover { background: var(--muted); }

/* ============================================================== gate === */
.gate {
  position: fixed; inset: 0; z-index: 100;
  display: grid; place-items: center;
  background: var(--bg); padding: 24px;
}
.gate-card {
  width: min(380px, 100%);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 36px 30px;
  text-align: center;
  box-shadow: var(--shadow);
}
.gate-mark { font-size: 34px; color: var(--accent); line-height: 1; }
.gate-card h1 { font-size: 19px; margin: 14px 0 6px; letter-spacing: -0.01em; }
.gate-card p { font-size: 13px; margin: 0 0 20px; }
.gate-card input {
  width: 100%; padding: 11px 13px; margin-bottom: 12px;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-family: var(--mono); font-size: 14px;
}
.gate-card input:focus { outline: none; border-color: var(--accent); }
.gate-card .btn { width: 100%; justify-content: center; }
.gate-error { color: var(--err); font-size: 12.5px; margin: 12px 0 0 !important; }

/* ============================================================= shell === */
.app { display: flex; height: 100dvh; overflow: hidden; }

.sidebar {
  width: var(--sidebar-w); flex: 0 0 var(--sidebar-w);
  display: flex; flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--border);
  overflow: hidden;
}

.side-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 16px 12px;
}
.brand { display: flex; align-items: center; gap: 9px; min-width: 0; }
.brand-mark {
  font-size: 20px; color: var(--accent); line-height: 1;
  width: 28px; height: 28px; display: grid; place-items: center;
  background: var(--accent-soft); border-radius: 8px; flex: none;
}
.brand-text { font-size: 14px; font-weight: 620; letter-spacing: -0.01em; white-space: nowrap; }
.brand-text em { font-style: normal; font-weight: 400; color: var(--muted); margin-left: 4px; }

.tabs {
  display: flex; gap: 3px; margin: 0 12px 12px;
  background: var(--bg); border-radius: 8px; padding: 3px;
}
.tab {
  flex: 1; padding: 7px 10px; border: 0; border-radius: 6px;
  background: transparent; color: var(--muted);
  font-family: inherit; font-size: 12.5px; font-weight: 560; cursor: pointer;
  transition: background .12s, color .12s;
}
.tab:hover { color: var(--text-dim); }
.tab.active { background: var(--raised); color: var(--text); }

.side-body { flex: 1; overflow-y: auto; padding: 0 12px; min-height: 0; }

.side-label {
  font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.09em;
  color: var(--muted); font-weight: 600;
  padding: 14px 4px 8px; display: flex; align-items: center; gap: 6px;
}
.count {
  background: var(--raised); border-radius: 20px; padding: 1px 7px;
  font-size: 10px; letter-spacing: 0;
}

.new-chat {
  width: 100%; justify-content: flex-start; gap: 8px;
  background: var(--accent); color: #fff; border-color: transparent;
  font-weight: 560;
}
.new-chat:hover { filter: brightness(1.1); }
.new-chat span { font-size: 15px; line-height: 1; }

.session-list { display: flex; flex-direction: column; gap: 2px; padding-bottom: 16px; }
.session {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px; border-radius: var(--radius-sm);
  cursor: pointer; border: 0; background: transparent;
  color: var(--text-dim); text-align: left; width: 100%;
  font-family: inherit; font-size: 13px;
  transition: background .1s;
}
.session:hover { background: var(--panel-2); color: var(--text); }
.session.active { background: var(--accent-soft); color: var(--text); }
.session-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.session-time { font-size: 10px; color: var(--muted); font-family: var(--mono); flex: none; }

.dash-nav { display: flex; flex-direction: column; gap: 2px; }
.dash-link {
  padding: 8px 10px; border: 0; border-radius: var(--radius-sm);
  background: transparent; color: var(--text-dim); text-align: left;
  font-family: inherit; font-size: 13px; cursor: pointer;
}
.dash-link:hover { background: var(--panel-2); color: var(--text); }
.dash-link.active { background: var(--accent-soft); color: var(--text); font-weight: 560; }

.side-foot {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 14px; border-top: 1px solid var(--border-soft);
}
.status-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--muted); flex: none;
}
.status-dot.ok { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.status-dot.err { background: var(--err); }
.side-foot span { flex: 1; color: var(--muted); }

/* =============================================================== main == */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--bg); }
.view { flex: 1; display: flex; flex-direction: column; min-height: 0; }

.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 22px; border-bottom: 1px solid var(--border);
  background: var(--bg); flex: none; min-height: 60px;
}
.topbar-title { flex: 1; min-width: 0; }
.eyebrow {
  display: block; font-size: 10px; text-transform: uppercase;
  letter-spacing: 0.1em; color: var(--muted); font-weight: 600;
}
.topbar-title h2 {
  margin: 1px 0 0; font-size: 15px; font-weight: 600; letter-spacing: -0.01em;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.topbar-actions { display: flex; align-items: center; gap: 6px; flex: none; }

.pill {
  font-size: 11px; padding: 4px 9px; border-radius: 20px;
  background: var(--raised); color: var(--text-dim); border: 1px solid var(--border-soft);
  white-space: nowrap; max-width: 220px; overflow: hidden; text-overflow: ellipsis;
}

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 9px 13px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--raised); color: var(--text);
  font-family: inherit; font-size: 13px; cursor: pointer;
  transition: background .12s, border-color .12s;
}
.btn:hover { background: var(--panel-2); border-color: var(--muted); }
.btn.primary { background: var(--accent); color: #fff; border-color: transparent; font-weight: 560; }
.btn.primary:hover { filter: brightness(1.1); background: var(--accent); }
.btn.small { padding: 5px 9px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.icon-btn {
  width: 30px; height: 30px; flex: none;
  display: grid; place-items: center;
  border: 0; border-radius: var(--radius-sm);
  background: transparent; color: var(--muted);
  font-size: 14px; cursor: pointer; font-family: inherit;
  transition: background .12s, color .12s;
}
.icon-btn:hover { background: var(--raised); color: var(--text); }
.icon-btn.danger:hover { background: rgba(255, 107, 107, .12); color: var(--err); }

/* ============================================================= thread === */
.thread { flex: 1; overflow-y: auto; padding: 26px 0 8px; scroll-behavior: smooth; }
.thread-inner, .msg-row { max-width: 780px; margin: 0 auto; padding: 0 26px; }

.empty { max-width: 620px; margin: 8vh auto 0; padding: 0 26px; text-align: center; }
.empty-mark {
  font-size: 40px; color: var(--accent); line-height: 1;
  width: 68px; height: 68px; margin: 0 auto 18px;
  display: grid; place-items: center;
  background: var(--accent-soft); border-radius: 18px;
}
.empty h3 { font-size: 21px; margin: 0 0 8px; letter-spacing: -0.02em; }
.empty p { font-size: 14px; margin: 0 auto 26px; max-width: 440px; }
.suggestions { display: grid; gap: 8px; text-align: left; }
.suggestion {
  padding: 12px 15px; border-radius: var(--radius);
  border: 1px solid var(--border); background: var(--panel);
  color: var(--text-dim); font-family: inherit; font-size: 13.5px;
  cursor: pointer; text-align: left;
  transition: border-color .12s, color .12s, transform .12s;
}
.suggestion:hover { border-color: var(--accent); color: var(--text); transform: translateY(-1px); }

.msg-row { margin-bottom: 22px; }
.msg-row.user { display: flex; justify-content: flex-end; }
.msg-user {
  background: var(--user-bubble); border-radius: 14px 14px 3px 14px;
  padding: 11px 15px; max-width: 78%;
  white-space: pre-wrap; word-wrap: break-word; font-size: 14.5px;
}

.msg-agent { display: flex; gap: 13px; align-items: flex-start; }
.avatar {
  width: 27px; height: 27px; flex: none; margin-top: 1px;
  display: grid; place-items: center; border-radius: 8px;
  background: var(--accent-soft); color: var(--accent-text);
  font-size: 14px; line-height: 1;
}
.avatar.thinking { animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .35; } }
.msg-body { flex: 1; min-width: 0; }

/* --- tool activity ----------------------------------------------------- */
.tools { display: flex; flex-direction: column; gap: 5px; margin-bottom: 12px; }
.tool {
  border: 1px solid var(--border-soft); border-radius: 8px;
  background: var(--panel); overflow: hidden; font-size: 12.5px;
}
.tool-head {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 11px; cursor: pointer; user-select: none;
}
.tool-head:hover { background: var(--panel-2); }
.tool-icon { font-size: 11px; width: 13px; text-align: center; flex: none; }
.tool.running .tool-icon { animation: spin 1.1s linear infinite; color: var(--accent-text); }
@keyframes spin { to { transform: rotate(360deg); } }
.tool.ok .tool-icon { color: var(--ok); }
.tool.failed .tool-icon { color: var(--err); }
.tool-name { font-family: var(--mono); color: var(--text-dim); font-weight: 500; }
.tool-preview {
  flex: 1; color: var(--muted); font-family: var(--mono); font-size: 11.5px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0;
}
.tool-caret { color: var(--muted); font-size: 9px; flex: none; transition: transform .15s; }
.tool.open .tool-caret { transform: rotate(90deg); }
.tool-detail {
  padding: 9px 11px 10px; font-family: var(--mono); font-size: 11.5px;
  color: var(--text-dim); white-space: pre-wrap; word-break: break-word;
  max-height: 340px; overflow: auto; border-top: 1px solid var(--border-soft);
  margin-top: 2px;
}
.tool-detail-label {
  font-family: var(--sans); font-size: 9.5px; font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.09em;
  color: var(--muted); margin: 9px 0 4px;
}
.tool-detail > .tool-detail-label:first-child { margin-top: 0; }

.thinking-block {
  border-left: 2px solid var(--border); padding: 2px 0 2px 13px;
  margin-bottom: 12px; color: var(--muted); font-size: 13px;
  white-space: pre-wrap; max-height: 190px; overflow: auto; font-style: italic;
}

/* --- markdown ---------------------------------------------------------- */
.md { font-size: 14.8px; }
.md > *:first-child { margin-top: 0; }
.md > *:last-child { margin-bottom: 0; }
.md h1, .md h2, .md h3, .md h4 { margin: 1.4em 0 .5em; line-height: 1.3; letter-spacing: -0.01em; }
.md h1 { font-size: 1.4em; } .md h2 { font-size: 1.22em; }
.md h3 { font-size: 1.1em; } .md h4 { font-size: 1em; }
.md p { margin: 0 0 .85em; }
.md ul, .md ol { margin: 0 0 .85em; padding-left: 1.5em; }
.md li { margin: .25em 0; }
.md li > ul, .md li > ol { margin: .25em 0; }
.md a { color: var(--accent-text); text-decoration: underline; text-underline-offset: 2px; }
.md strong { font-weight: 640; }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 1.4em 0; }
.md blockquote {
  margin: 0 0 .85em; padding: 2px 0 2px 14px;
  border-left: 3px solid var(--border); color: var(--text-dim);
}
.md code {
  font-family: var(--mono); font-size: .86em;
  background: var(--raised); padding: 1.5px 5px; border-radius: 4px;
  border: 1px solid var(--border-soft);
}
.md pre {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 13px 15px; overflow-x: auto;
  margin: 0 0 .85em; position: relative;
}
.md pre code { background: none; border: 0; padding: 0; font-size: 12.6px; line-height: 1.6; }
.md li.task {
  list-style: none; margin-left: -1.2em;
  display: flex; align-items: flex-start; gap: 9px;
}
.md .task-box {
  flex: none; width: 15px; height: 15px; margin-top: 4px;
  border: 1.5px solid var(--muted); border-radius: 4px;
  display: inline-grid; place-items: center;
  font-size: 10px; line-height: 1; color: transparent;
}
.md .task-box.checked {
  background: var(--accent); border-color: var(--accent); color: #fff;
}
.md table {
  border-collapse: collapse; width: 100%; margin: 0 0 .85em;
  font-size: 13.5px; display: block; overflow-x: auto;
}
.md th, .md td { border: 1px solid var(--border); padding: 7px 11px; text-align: left; }
.md th { background: var(--raised); font-weight: 600; }

.copy-code {
  position: absolute; top: 7px; right: 7px;
  padding: 3px 8px; font-size: 10.5px; font-family: var(--mono);
  border: 1px solid var(--border); border-radius: 4px;
  background: var(--raised); color: var(--muted);
  cursor: pointer; opacity: 0; transition: opacity .12s;
}
.md pre:hover .copy-code { opacity: 1; }
.copy-code:hover { color: var(--text); border-color: var(--muted); }

.msg-error {
  border: 1px solid var(--err); border-radius: var(--radius);
  background: rgba(255, 107, 107, .07); color: var(--err);
  padding: 11px 14px; font-size: 13.5px;
}
.msg-foot { margin-top: 8px; display: flex; gap: 12px; align-items: center; }

.caret {
  display: inline-block; width: 7px; height: 15px; margin-left: 2px;
  background: var(--accent); vertical-align: text-bottom;
  animation: blink 1s step-end infinite;
}
@keyframes blink { 50% { opacity: 0; } }

/* =========================================================== composer === */
.composer-wrap { flex: none; padding: 10px 26px 18px; background: var(--bg); }
.composer {
  max-width: 780px; margin: 0 auto;
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 14px; padding: 8px 8px 8px 15px;
  transition: border-color .14s;
}
.composer:focus-within { border-color: var(--accent); }
.composer textarea {
  flex: 1; border: 0; background: none; color: var(--text);
  font-family: inherit; font-size: 14.8px; line-height: 1.55;
  resize: none; outline: none; padding: 7px 0; min-height: 37px; max-height: 210px;
}
.composer textarea::placeholder { color: var(--muted); }
.send {
  width: 33px; height: 33px; flex: none; border: 0; border-radius: 9px;
  background: var(--accent); color: #fff; font-size: 15px;
  cursor: pointer; display: grid; place-items: center;
  transition: filter .12s, opacity .12s;
}
.send:hover { filter: brightness(1.12); }
.send:disabled { opacity: .3; cursor: not-allowed; }
.send.stop { background: var(--err); font-size: 10px; }
.composer-meta { max-width: 780px; margin: 7px auto 0; text-align: center; color: var(--muted); min-height: 14px; }

/* ========================================================== dashboard === */
.dash-body { flex: 1; overflow-y: auto; padding: 26px; }
.dash-inner { max-width: 1000px; margin: 0 auto; }

.stat-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(178px, 1fr));
  gap: 12px; margin-bottom: 26px;
}
.stat {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 15px 17px;
}
.stat-label {
  font-size: 10px; text-transform: uppercase; letter-spacing: 0.09em;
  color: var(--muted); font-weight: 600; margin-bottom: 7px;
}
.stat-value {
  font-size: 22px; font-weight: 600; letter-spacing: -0.02em; line-height: 1.15;
  overflow: hidden; text-overflow: ellipsis;
}
.stat-value.sm { font-size: 15px; font-weight: 500; font-family: var(--mono); }
.stat-sub { font-size: 11.5px; color: var(--muted); margin-top: 4px; }
.stat-value.ok { color: var(--ok); } .stat-value.err { color: var(--err); }

.card {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); margin-bottom: 18px; overflow: hidden;
}
.card-head {
  display: flex; align-items: center; gap: 10px;
  padding: 13px 17px; border-bottom: 1px solid var(--border-soft);
}
.card-head h3 { margin: 0; font-size: 13.5px; font-weight: 620; flex: 1; }
.card-body { padding: 6px 0; }
.card-body.pad { padding: 15px 17px; }

.row {
  display: flex; align-items: center; gap: 12px;
  padding: 11px 17px; border-bottom: 1px solid var(--border-soft);
}
.row:last-child { border-bottom: 0; }
.row-main { flex: 1; min-width: 0; }
.row-title { font-size: 13.5px; font-weight: 540; display: flex; align-items: center; gap: 8px; }
.row-desc {
  font-size: 12.3px; color: var(--muted); margin-top: 2px;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.row-actions { display: flex; gap: 5px; flex: none; }

.tag {
  font-size: 10px; font-family: var(--mono); padding: 2px 7px; border-radius: 4px;
  background: var(--raised); color: var(--text-dim); border: 1px solid var(--border-soft);
  white-space: nowrap;
}
.tag.on { background: rgba(62, 207, 142, .13); color: var(--ok); border-color: transparent; }
.tag.off { background: var(--raised); color: var(--muted); }
.tag.blue { background: var(--accent-soft); color: var(--accent-text); border-color: transparent; }

.kv { display: grid; grid-template-columns: minmax(140px, 210px) 1fr; gap: 3px 18px; font-size: 13px; }
.kv dt { color: var(--muted); padding: 5px 0; }
.kv dd {
  margin: 0; padding: 5px 0; font-family: var(--mono); font-size: 12.4px;
  word-break: break-word; border-bottom: 1px solid var(--border-soft);
}
.kv dt { border-bottom: 1px solid var(--border-soft); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  font-size: 11.5px; font-family: var(--mono);
  padding: 4px 9px; border-radius: 5px;
  background: var(--raised); border: 1px solid var(--border-soft); color: var(--text-dim);
}

.dash-empty { padding: 26px 17px; text-align: center; color: var(--muted); font-size: 13px; }
.loading { padding: 30px; text-align: center; color: var(--muted); font-size: 13px; }
.search-input {
  padding: 6px 11px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
  font-family: inherit; font-size: 12.5px; width: 190px;
}
.search-input:focus { outline: none; border-color: var(--accent); }

/* ============================================================= toasts === */
.toasts {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  z-index: 200; display: flex; flex-direction: column; gap: 8px; align-items: center;
  pointer-events: none;
}
.toast {
  background: var(--raised); border: 1px solid var(--border);
  border-radius: 8px; padding: 9px 15px; font-size: 13px;
  box-shadow: var(--shadow); animation: rise .2s ease-out;
  max-width: 90vw;
}
.toast.err { border-color: var(--err); color: var(--err); }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } }

/* ========================================================= responsive === */
.only-mobile { display: none; }

@media (max-width: 860px) {
  .only-mobile { display: grid; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 50;
    transform: translateX(-100%); transition: transform .2s ease;
    box-shadow: var(--shadow);
  }
  .sidebar.open { transform: translateX(0); }
  .topbar { padding: 12px 15px; }
  .thread-inner, .msg-row, .empty { padding-left: 15px; padding-right: 15px; }
  .composer-wrap { padding: 8px 15px 14px; }
  .dash-body { padding: 17px 15px; }
  .msg-user { max-width: 88%; }
  .kv { grid-template-columns: 1fr; gap: 0; }
  .kv dt { border-bottom: 0; padding-bottom: 0; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
  .thread { scroll-behavior: auto; }
}
