:root {
  --bg: #f5f3ee;
  --panel: #fbfaf7;
  --card: #ffffff;
  --ink: #1d1c1a;
  --ink-2: #4a4843;
  --muted: #7b776f;
  --line: #e4e0d7;
  --line-2: #d6d1c5;
  --accent: #1d5c4d;
  --accent-ink: #f5f3ee;
  --accent-soft: #e3eee9;
  --amber: #f2b544;
  --danger: #b4412f;
  --radius: 12px;
  --font: ui-sans-serif, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161614; --panel: #1c1c1a; --card: #232320; --ink: #ecebe6; --ink-2: #c9c6bd; --muted: #9a968c;
    --line: #2e2d29; --line-2: #3a3934; --accent: #5fb39b; --accent-ink: #10201b; --accent-soft: #1f2e29;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.5 var(--font); -webkit-font-smoothing: antialiased; }
[hidden] { display: none !important; }
code { font-family: var(--mono); font-size: .86em; background: color-mix(in srgb, var(--line) 60%, transparent); padding: .1em .35em; border-radius: 5px; }
h1, h2 { margin: 0; letter-spacing: -.01em; }
h1 { font-size: 17px; font-weight: 650; }
h2 { font-size: 15px; font-weight: 650; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--danger); font-size: 14px; margin: 0; }
input, textarea, select { font: inherit; color: inherit; }
input, textarea {
  width: 100%; background: var(--card); border: 1px solid var(--line-2); border-radius: 10px; padding: 10px 12px; outline: none;
  transition: border-color .15s, box-shadow .15s;
}
input:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent); }
textarea { resize: none; }

.btn { border: 1px solid transparent; border-radius: 10px; padding: 9px 16px; font: inherit; font-weight: 600; cursor: pointer; transition: background .15s, opacity .15s; }
.btn:disabled { opacity: .5; cursor: default; }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-primary:hover:not(:disabled) { background: color-mix(in srgb, var(--accent) 88%, black); }
.btn-ghost { background: transparent; border-color: var(--line-2); color: var(--ink-2); }
.btn-ghost:hover { background: var(--card); }
.btn-xs { padding: 3px 10px; font-size: 12.5px; border-radius: 8px; }
.btn-sm { padding: 6px 14px; font-size: 14px; }

.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; letter-spacing: -.01em; }
.brand-lg { font-size: 22px; }
.logo { width: 26px; height: 26px; border-radius: 8px; background: var(--accent); position: relative; flex: none; }
.logo::before, .logo::after { content: ""; position: absolute; width: 7px; height: 7px; border-radius: 50%; background: var(--amber); top: 12px; }
.logo::before { left: 4px; } .logo::after { right: 4px; }
.brand-lg .logo { width: 32px; height: 32px; }
.brand-lg .logo::before, .brand-lg .logo::after { top: 15px; }

/* gate */
.gate { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.gate-card { width: min(420px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 28px; display: grid; gap: 12px; }
.gate-card label { font-weight: 600; font-size: 14px; margin-top: 6px; }
.gate-card .hint { margin: 0; font-size: 13px; color: var(--muted); }
.gate-card p { margin: 0; }

/* layout */
.app { height: 100dvh; display: grid; grid-template-columns: 250px minmax(0, 1fr) 340px; }
.pane { min-height: 0; display: flex; flex-direction: column; }
.pane-peers { background: var(--panel); border-right: 1px solid var(--line); padding: 18px 16px; gap: 6px; }
.pane-knowledge { background: var(--panel); border-left: 1px solid var(--line); padding: 18px 16px; gap: 12px; }
.section-label { font-size: 11.5px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); font-weight: 650; margin: 18px 0 6px; }

.peers { list-style: none; padding: 0; margin: 0; display: grid; gap: 2px; }
.peer { display: flex; align-items: center; gap: 10px; padding: 7px 8px; border-radius: 10px; }
.peer:hover { background: var(--card); }
.peer-name { font-weight: 600; font-size: 14px; line-height: 1.25; }
.peer-meta { font-size: 12px; color: var(--muted); }
.tag { font-size: 10.5px; font-weight: 650; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); border: 1px solid var(--line-2); border-radius: 5px; padding: 0 5px; margin-left: 6px; vertical-align: 1px; }
.side-foot { margin-top: auto; }
.side-foot p { margin: 0 0 8px; }
.copy-row { display: flex; gap: 6px; align-items: center; }
.copy-row code { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: block; padding: 5px 8px; }
.signout { margin-top: 14px; }

.avatar { width: 34px; height: 34px; border-radius: 10px; flex: none; display: grid; place-items: center; font-weight: 700; font-size: 13px; color: #fff; letter-spacing: .02em; }
.avatar.sm { width: 28px; height: 28px; border-radius: 8px; font-size: 11.5px; }

/* chat */
.pane-chat { background: var(--bg); }
.chat-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 24px; border-bottom: 1px solid var(--line); }
.chat-head p { margin: 0; }
.conn { display: inline-flex; align-items: center; gap: 7px; font-size: 12.5px; color: var(--muted); border: 1px solid var(--line); border-radius: 999px; padding: 3px 10px; }
.conn .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--amber); }
.conn.live .dot { background: #2f9e6f; box-shadow: 0 0 0 3px color-mix(in srgb, #2f9e6f 20%, transparent); }
.conn.down .dot { background: var(--danger); }

.messages { flex: 1; overflow-y: auto; padding: 18px 24px 8px; display: flex; flex-direction: column; }
.empty { margin: auto; text-align: center; max-width: 320px; }
.empty-title { font-weight: 650; margin: 0 0 4px; }
.empty p { margin: 0; }
.day { align-self: center; font-size: 12px; color: var(--muted); margin: 14px 0 8px; padding: 2px 10px; border: 1px solid var(--line); border-radius: 999px; background: var(--panel); }
.msg { display: grid; grid-template-columns: 34px minmax(0, 1fr); column-gap: 12px; padding: 6px 8px; border-radius: 10px; margin: 0 -8px; }
.msg:hover { background: color-mix(in srgb, var(--card) 70%, transparent); }
.msg.first { margin-top: 10px; }
.msg:not(.first) .avatar { visibility: hidden; height: 0; }
.msg-head { display: flex; align-items: baseline; gap: 8px; }
.msg-name { font-weight: 650; font-size: 14.5px; }
.msg-time { font-size: 12px; color: var(--muted); }
.msg-body { white-space: pre-wrap; overflow-wrap: anywhere; color: var(--ink); }
.msg-body pre { background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; overflow-x: auto; white-space: pre; margin: 6px 0; font: 13px/1.5 var(--mono); }
.msg-body a { color: var(--accent); }
.msg.new { animation: flash 1.2s ease-out; }
@keyframes flash { from { background: var(--accent-soft); } to { background: transparent; } }

.composer { padding: 10px 24px 18px; }
.composer-box { background: var(--card); border: 1px solid var(--line-2); border-radius: 14px; padding: 4px; transition: border-color .15s, box-shadow .15s; }
.composer-box:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 15%, transparent); }
.composer textarea { border: 0; box-shadow: none !important; background: transparent; max-height: 180px; padding: 10px 12px 6px; }
.composer-bar { display: flex; align-items: center; justify-content: space-between; padding: 2px 6px 4px 12px; gap: 12px; }
.as-label { font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 8px; }
.as-label select { background: var(--panel); border: 1px solid var(--line-2); border-radius: 8px; padding: 4px 8px; font-size: 13px; font-weight: 600; color: var(--ink); }
.btn-send { padding: 7px 18px; }

/* knowledge */
.k-head { display: flex; align-items: center; justify-content: space-between; }
.k-form { display: grid; gap: 8px; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; }
.k-form-bar { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.k-search { padding: 8px 12px; }
.k-list { list-style: none; padding: 0; margin: 0; overflow-y: auto; display: grid; gap: 8px; align-content: start; flex: 1; }
.k-item { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 11px 13px; cursor: pointer; transition: border-color .15s; }
.k-item:hover { border-color: var(--line-2); }
.k-title { font-weight: 650; font-size: 14px; margin: 0 0 3px; }
.k-body { margin: 0; font-size: 13.5px; color: var(--ink-2); white-space: pre-wrap; overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.k-item.open .k-body { -webkit-line-clamp: unset; display: block; }
.k-meta { font-size: 12px; color: var(--muted); margin-top: 6px; display: flex; align-items: center; gap: 6px; }
.k-item.new { animation: flash 1.2s ease-out; }
.k-empty { text-align: center; margin-top: 8px; }

.tabs { display: none; }
.toast { position: fixed; left: 50%; bottom: 84px; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 8px 14px; border-radius: 10px; font-size: 14px; z-index: 10; }

@media (max-width: 1100px) {
  .app { grid-template-columns: 220px minmax(0, 1fr) 300px; }
}
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; grid-template-rows: minmax(0, 1fr) auto; }
  .pane { grid-row: 1; grid-column: 1; display: none; border: 0; }
  .pane.active { display: flex; }
  .chat-head, .messages, .composer { padding-left: 14px; padding-right: 14px; }
  .tabs { display: flex; grid-row: 2; border-top: 1px solid var(--line); background: var(--panel); padding-bottom: env(safe-area-inset-bottom); }
  .tabs button { flex: 1; background: none; border: 0; padding: 12px 0; font: inherit; font-weight: 600; color: var(--muted); cursor: pointer; }
  .tabs button.active { color: var(--accent); box-shadow: inset 0 2px 0 var(--accent); }
}
