:root {
  --cream: #faf6ee;
  --paper: #fffdf8;
  --lav: #9a5a86;
  --lav-soft: #f2e6ee;
  --lav-deep: #6b3560;
  --ink: #2a2129;
  --ink-soft: #5f5560;
  --line: #e7dfd2;
  --rail-w: 260px;
  --err: #a8323b;
  --err-bg: #fbecec;
  --ok: #2f7a55;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(35,33,43,.05), 0 8px 24px rgba(35,33,43,.06);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", sans-serif;
  color: var(--ink);
  background: var(--cream);
  line-height: 1.6;
}
* { box-sizing: border-box; }
html, body { margin: 0; overflow-x: hidden; }
body { min-height: 100vh; background: radial-gradient(1200px 600px at 100% -10%, var(--lav-soft), transparent 60%), var(--cream); }
img, video, audio { max-width: 100%; }
a { color: var(--lav-deep); }
:focus-visible { outline: 3px solid var(--lav); outline-offset: 2px; border-radius: 6px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.boot { padding: 3rem; text-align: center; }

/* Login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem; }
.login-card { width: min(420px, 100%); background: var(--paper); border: 1px solid var(--line); border-radius: 22px; padding: 2.2rem 2rem; box-shadow: var(--shadow); }
.login-card h1 { margin: .6rem 0 .2rem; font-size: 1.6rem; letter-spacing: .02em; }

/* Shell */
.shell { display: grid; grid-template-columns: var(--rail-w) minmax(0, 1fr); grid-template-rows: auto 1fr; grid-template-areas: "side top" "side main"; min-height: 100vh; }
.topbar { grid-area: top; }
.sidebar { grid-area: side; }
.main { grid-area: main; }
.scrim { display: none; }
.sidebar { background: var(--paper); border-right: 1px solid var(--line); padding: .9rem .7rem; display: flex; flex-direction: column; gap: .6rem; position: sticky; top: 0; height: 100vh; height: 100dvh; overflow: hidden; min-width: 0; }

/* Rail: new chat, recent conversations, tools + account disclosures */
.new-chat { display: flex; align-items: center; gap: .55rem; width: 100%; padding: .6rem .8rem; border-radius: 12px; background: var(--ink); color: var(--cream); border: 1px solid var(--ink); font-weight: 600; text-align: left; }
.new-chat:hover { background: var(--lav-deep); border-color: var(--lav-deep); }
.new-chat-icon { display: inline-grid; place-items: center; width: 1.3rem; height: 1.3rem; border-radius: 6px; background: rgba(255,255,255,.16); font-size: 1rem; line-height: 1; }
.conversation-dock { flex: 1 1 auto; min-height: 0; overflow-y: auto; overflow-x: hidden; display: flex; flex-direction: column; gap: .15rem; margin: 0 -.2rem; padding: 0 .2rem; }
.conversation-dock:empty { flex: 1 1 auto; }
.dock-back { all: unset; box-sizing: border-box; cursor: pointer; padding: .5rem .7rem; border-radius: 10px; color: var(--lav-deep); font-weight: 600; }
.dock-back:hover { background: var(--lav-soft); }
.dock-back:focus-visible { outline: 3px solid var(--lav); }
.rail-menu { border: 0; background: transparent; padding: 0; }
.rail-menu > summary { list-style: none; display: flex; align-items: center; gap: .55rem; padding: .5rem .7rem; border-radius: 10px; color: var(--ink-soft); font-weight: 500; min-width: 0; }
.rail-menu > summary::-webkit-details-marker { display: none; }
.rail-menu > summary::after { content: "▸"; margin-left: auto; font-size: .75rem; transition: transform .15s; }
.rail-menu[open] > summary::after { transform: rotate(90deg); }
.rail-menu > summary:hover { background: var(--lav-soft); color: var(--ink); }
.rail-menu-icon { width: 1.3rem; text-align: center; }
.tools-list { padding: .2rem 0 .3rem .6rem; max-height: 40vh; overflow-y: auto; }
.tools-list button { padding: .4rem .7rem; font-size: .92rem; }
.account-menu { border-top: 1px solid var(--line); padding-top: .4rem; }
.account-dot { width: .6rem; height: .6rem; border-radius: 50%; background: var(--ok); flex: none; }
.account-dot.bad { background: var(--err); }
.account-name { color: var(--ink); font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.account-body { display: flex; flex-direction: column; gap: .5rem; padding: .4rem .3rem .2rem; }
.account-body .muted { font-size: .8rem; margin: 0; }
.account-body .account-badge { margin: 0; }
.account-body .actions button { flex: 1 1 auto; padding: .4rem .7rem; }
.nav { display: flex; flex-direction: column; gap: .2rem; }
.nav-title { font-size: .75rem; color: var(--ink-soft); margin: .8rem .6rem .2rem; letter-spacing: .08em; }
.nav button { all: unset; cursor: pointer; padding: .55rem .8rem; border-radius: 10px; color: var(--ink-soft); display: block; }
.nav button:hover { background: var(--lav-soft); color: var(--ink); }
.nav button[aria-current="page"] { background: var(--ink); color: var(--cream); }
.nav button:focus-visible { outline: 3px solid var(--lav); }
.status { font-size: .85rem; padding: .5rem .7rem; border-radius: 10px; background: var(--lav-soft); }
.status.bad { background: var(--err-bg); color: var(--err); }
.account-badge { font-size: .85rem; margin: 0 0 .5rem; padding: .4rem .7rem; border-radius: 10px; border: 1px solid var(--lav-soft); }
.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: .2rem; flex: none; max-height: 60%; overflow-y: auto; }
.topbar, .topbar[hidden], .shell-menu-toggle { display: none; }
.main { min-width: 0; padding: 1.6rem clamp(1rem, 3vw, 2.5rem); }

/* Chat route: main fills the viewport; chat.css owns the scrolling transcript + sticky composer */
.shell.route-chat { height: 100vh; height: 100dvh; min-height: 0; }
.shell.route-chat .main { padding: 0; display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
.shell.route-chat .main > .chat-page { flex: 1 1 auto; min-height: 0; gap: 0; }
.shell.route-chat .main > .chat-page > .loading { padding: 1.5rem; }

@media (max-width: 820px) {
  .shell { grid-template-columns: minmax(0, 1fr); grid-template-areas: "top" "main"; }
  .shell:not(.open) .sidebar { visibility: hidden; }
  .shell.open .sidebar { visibility: visible; }
  .topbar { display: flex; align-items: center; justify-content: space-between; gap: .5rem; padding: .5rem .8rem; background: var(--paper); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20; min-width: 0; }
  .shell-menu-toggle { display: inline-flex; flex: none; align-items: center; white-space: nowrap; }
  .topbar > button { flex: none; padding: .45rem .8rem; }
  .shell.route-chat { grid-template-rows: auto minmax(0, 1fr); }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: min(var(--rail-w), 85vw); height: 100dvh; z-index: 30; transform: translateX(-105%); transition: transform .2s; box-shadow: var(--shadow); }
  .shell.open .sidebar { transform: none; }
  .scrim { display: none; }
  .shell.open .scrim { display: block; position: fixed; inset: 0; background: rgba(35,33,43,.3); z-index: 25; }
  .main { padding: 1rem; }

  /* Active mobile chat only (html.vv-chat, toggled by app.js): pin the shell to the visual viewport
     so a late keyboard/toolbar resize can't leave the header/composer offset. Only the transcript
     and the drawer scroll; root/body never carry stale overflow. */
  html.vv-chat, html.vv-chat body { height: 100%; min-height: 0; overflow: hidden; overscroll-behavior: none; }
  html.vv-chat { --vv-fallback-h: 100vh; }
  @supports (height: 100dvh) { html.vv-chat { --vv-fallback-h: 100dvh; } }
  html.vv-chat .shell.route-chat {
    position: fixed; left: 0; right: 0; top: var(--vv-top, 0px);
    height: var(--vv-h, var(--vv-fallback-h));
  }
  html.vv-chat .sidebar { top: var(--vv-top, 0px); bottom: auto; height: var(--vv-h, var(--vv-fallback-h)); }
}

/* Shared */
.view { display: flex; flex-direction: column; gap: 1rem; min-width: 0; }
.view > h2, .view h2 { margin: 0; font-size: 1.35rem; }
.panel { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.1rem; box-shadow: var(--shadow); min-width: 0; }
.row { display: flex; flex-wrap: wrap; gap: .75rem; align-items: flex-end; }
.row > * { min-width: 0; }
.grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(min(260px, 100%), 1fr)); }
.field { display: flex; flex-direction: column; gap: .3rem; flex: 1 1 180px; min-width: 0; }
.field > label, label { font-size: .88rem; font-weight: 600; color: var(--ink); }
.actions { display: flex; flex-wrap: wrap; gap: .6rem; align-items: center; }
.muted { color: var(--ink-soft); font-size: .9rem; }
.error { color: var(--err); background: var(--err-bg); border: 1px solid #f0c9cc; padding: .6rem .8rem; border-radius: 10px; overflow-wrap: anywhere; }
.error:empty { display: none; }

input, textarea, select { font: inherit; color: var(--ink); background: #fff; border: 1px solid var(--line); border-radius: 10px; padding: .55rem .7rem; width: 100%; max-width: 100%; }
input[type="checkbox"], input[type="radio"] { width: auto; }
textarea { min-height: 6rem; resize: vertical; }
input:focus, textarea:focus, select:focus { border-color: var(--lav); outline: 3px solid var(--lav-soft); }

.button, button { font: inherit; cursor: pointer; border-radius: 10px; padding: .5rem 1rem; border: 1px solid var(--line); background: var(--paper); color: var(--ink); }
button:hover, .button:hover { border-color: var(--lav); }
button.primary, .button.primary { background: var(--ink); color: var(--cream); border-color: var(--ink); }
button.primary:hover { background: var(--lav-deep); border-color: var(--lav-deep); }
button.secondary, .button.secondary { background: var(--lav-soft); border-color: transparent; color: var(--lav-deep); }
button:disabled { opacity: .55; cursor: not-allowed; }
.button { display: inline-block; text-decoration: none; }

.result { display: flex; flex-direction: column; gap: .7rem; min-width: 0; overflow-wrap: anywhere; }
.result .stream-text { white-space: pre-wrap; }
.result figure { margin: 0; }
.result figcaption { font-size: .82rem; color: var(--ink-soft); }
details { border: 1px solid var(--line); border-radius: 10px; background: #fff; padding: .3rem .7rem; min-width: 0; }
summary { cursor: pointer; font-weight: 600; padding: .3rem 0; }
pre { background: #f6f3ec; border-radius: 8px; padding: .7rem; overflow: auto; max-height: 420px; font-size: .82rem; white-space: pre-wrap; word-break: break-word; margin: .4rem 0; }
.loading::after { content: " …"; animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: .2; } }

.chat-transcript { display: flex; flex-direction: column; gap: .7rem; min-height: 200px; max-height: 65vh; overflow-y: auto; padding: .5rem; }
.bubble { max-width: min(46rem, 92%); padding: .7rem .95rem; border-radius: 16px; background: #fff; border: 1px solid var(--line); white-space: pre-wrap; overflow-wrap: anywhere; }
.bubble.user { align-self: flex-end; background: var(--lav-soft); border-color: transparent; }
.bubble.assistant { align-self: flex-start; }
.bubble.error { align-self: stretch; }

/* Nav states */
.nav button { transition: background .15s, color .15s; position: relative; }
.nav button[aria-current="page"]::before { content: ""; position: absolute; left: -.5rem; top: 25%; bottom: 25%; width: 3px; border-radius: 3px; background: var(--lav); }
.nav button:active { transform: translateY(1px); }
.status { display: flex; align-items: center; gap: .4rem; color: var(--lav-deep); font-weight: 600; }
.status.ok, .status:not(.bad) { color: var(--ok); }

/* Explorer */
.exp-split { display: grid; grid-template-columns: minmax(220px, 300px) minmax(0, 1fr); gap: 1rem; align-items: start; }
.exp-split > * { min-width: 0; }
@media (max-width: 960px) { .exp-split { grid-template-columns: minmax(0, 1fr); } }
.exp-op { display: flex; align-items: center; gap: .55rem; width: 100%; text-align: left; padding: .45rem .6rem; border: 1px solid transparent; border-radius: 10px; background: transparent; color: var(--ink); overflow-wrap: anywhere; }
.exp-op:hover { background: var(--lav-soft); border-color: transparent; }
.exp-op[aria-current="true"], .exp-op[aria-pressed="true"], .exp-op.active { background: var(--paper); border-color: var(--lav); box-shadow: inset 3px 0 0 var(--lav); }
.method { display: inline-block; min-width: 3.6em; text-align: center; font: 700 .7rem/1.6 ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: .04em; padding: 0 .4rem; border-radius: 6px; background: #eeeae0; color: var(--ink-soft); flex: none; }
.method.get { background: #e3f1ea; color: var(--ok); }
.method.post { background: var(--lav-soft); color: var(--lav-deep); }
.method.delete { background: var(--err-bg); color: var(--err); }
.method.put, .method.patch { background: #fbf0dc; color: #8a5a12; }
.group-list, .history-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .25rem; }
.group-list details { border: 0; background: transparent; padding: 0; }
.group-list summary { font-size: .8rem; color: var(--ink-soft); letter-spacing: .06em; padding: .4rem .3rem; }
.group-list ul { list-style: none; margin: 0 0 .4rem; padding: 0 0 0 .3rem; border-left: 1px solid var(--line); }
.history-list > li { background: var(--paper); border: 1px solid var(--line); border-radius: 12px; padding: .7rem .9rem; display: flex; flex-wrap: wrap; gap: .4rem .8rem; align-items: center; transition: border-color .15s; }
.history-list > li:hover { border-color: var(--lav); }
.history-list .path { font-family: ui-monospace, Menlo, monospace; font-size: .85rem; overflow-wrap: anywhere; flex: 1 1 200px; }
.history-list time, .history-list .meta { font-size: .8rem; color: var(--ink-soft); }
.badge { display: inline-block; font-size: .75rem; font-weight: 600; padding: .05rem .5rem; border-radius: 999px; background: var(--lav-soft); color: var(--lav-deep); }
.badge.ok { background: #e3f1ea; color: var(--ok); }
.badge.bad, .badge.error { background: var(--err-bg); color: var(--err); }
