    :root {
      --bg: #0d0a06;
      --sidebar: #0a0805;
      --panel: #1c1610;
      --panel-hover: #251c14;
      --border: #2a2218;
      --text: #f0e8d8;
      --text-muted: #a39577;
      --accent: #ffb000;
      --accent-glow: rgba(255, 176, 0, 0.35);
      --user-bubble: #1c1610;
      --input-bg: #13100b;
      --danger: #ef5350;
      --radius: 2px;
      --radius-sm: 2px;
      --sidebar-w: 260px;
      --font-display: "VT323", "Geist Mono", ui-monospace, monospace;
      --font-mono: "Geist Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
    }
    * { box-sizing: border-box; }
    html, body { height: 100%; margin: 0; }
    body {
      background: var(--bg);
      color: var(--text);
      font-family: "Geist", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
      font-size: 16px;
      line-height: 1.6;
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
      display: flex;
      overflow: hidden;
    }

    /* ---------- Sidebar ---------- */
    .sidebar {
      width: var(--sidebar-w);
      background: var(--sidebar);
      border-right: 1px solid var(--border);
      display: flex;
      flex-direction: column;
      flex-shrink: 0;
      transition: margin-left 0.2s ease;
    }
    .sidebar-header {
      padding: 12px;
      display: flex;
      gap: 8px;
    }
    .new-chat-btn {
      flex: 1;
      background: transparent;
      color: var(--text);
      border: 1px solid var(--border);
      border-radius: var(--radius-sm);
      padding: 10px 12px;
      font: inherit;
      font-weight: 500;
      cursor: pointer;
      display: flex;
      align-items: center;
      gap: 8px;
      justify-content: flex-start;
      text-align: left;
    }
    .new-chat-btn:hover { background: var(--panel-hover); }
    .icon-btn {
      background: transparent;
      color: var(--text);
      border: 1px solid var(--border);
      border-radius: var(--radius-sm);
      width: 38px;
      height: 38px;
      padding: 0;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .icon-btn:hover { background: var(--panel-hover); }
    .sidebar-search {
      position: relative;
      padding: 0 12px 8px;
    }
    .sidebar-search input {
      width: 100%;
      background: var(--panel);
      color: var(--text);
      border: 1px solid var(--border);
      border-radius: var(--radius-sm);
      padding: 8px 42px 8px 12px;
      font: inherit;
      font-size: 13px;
      outline: 0;
    }
    .sidebar-search input:focus { border-color: #3d3120; }
    .sidebar-search input::-webkit-search-cancel-button {
      -webkit-appearance: none;
    }
    .sidebar-search kbd {
      position: absolute;
      right: 18px;
      top: 50%;
      transform: translateY(-50%);
      margin-top: -4px;  /* compensate for the 8px bottom padding */
      background: #13100b;
      border: 1px solid var(--border);
      color: var(--text-muted);
      padding: 1px 5px;
      font-size: 10px;
      border-radius: 2px;
      font-family: "Geist Mono", monospace;
      pointer-events: none;
    }
    .sidebar-search input:focus + kbd { display: none; }

    .search-result {
      padding: 8px 10px;
      border-radius: var(--radius-sm);
      cursor: pointer;
      color: var(--text);
      font-size: 13px;
      border-bottom: 1px solid transparent;
    }
    .search-result:hover { background: var(--panel-hover); }
    .search-result .meta {
      color: var(--text-muted);
      font-size: 11px;
      display: flex;
      justify-content: space-between;
      margin-bottom: 2px;
    }
    .search-result .snippet {
      line-height: 1.4;
      word-break: break-word;
      color: var(--text);
    }
    .search-result .snippet mark {
      background: rgba(255, 176, 0, 0.28);
      color: var(--text);
      border-radius: 2px;
      padding: 0 2px;
    }
    .search-empty {
      color: var(--text-muted);
      font-style: italic;
      padding: 16px 12px;
      font-size: 13px;
      text-align: center;
    }

    .conv-list {
      flex: 1;
      overflow-y: auto;
      padding: 4px 8px 12px;
    }
    .conv-group-label {
      color: var(--text-muted);
      font-size: 11px;
      font-weight: 600;
      text-transform: uppercase;
      letter-spacing: 0.06em;
      padding: 14px 12px 6px;
    }
    .conv-group-label:first-child { padding-top: 8px; }
    .conv-item {
      padding: 9px 12px;
      border-radius: var(--radius-sm);
      cursor: pointer;
      display: flex;
      align-items: center;
      gap: 8px;
      color: var(--text);
      font-size: 14px;
      white-space: nowrap;
      overflow: hidden;
      position: relative;
    }
    .conv-item:hover { background: var(--panel); }
    .conv-item.active {
      background: var(--panel-hover);
      font-weight: 500;
    }
    .conv-item .title {
      flex: 1;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    .conv-item .pin-dot {
      color: var(--text-muted);
      flex-shrink: 0;
    }
    .conv-item .menu {
      opacity: 0;
      background: transparent;
      color: var(--text-muted);
      border: 0;
      padding: 2px 6px;
      cursor: pointer;
      border-radius: 2px;
    }
    .conv-item:hover .menu, .conv-item.active .menu { opacity: 1; }
    .conv-item .menu:hover { color: var(--danger); }
    .conv-rename-input {
      flex: 1;
      min-width: 0;
      background: #13100b;
      color: var(--text);
      border: 1px solid var(--accent);
      border-radius: 2px;
      padding: 2px 6px;
      font: inherit;
      font-size: 14px;
      outline: none;
    }

    /* ---------- Main column ---------- */
    .main {
      flex: 1;
      display: flex;
      flex-direction: column;
      min-width: 0;
    }
    .topbar {
      display: flex;
      align-items: center;
      gap: 8px;
      padding: 10px 16px;
      height: 52px;
      background: rgba(13, 10, 6, 0.78);
      backdrop-filter: saturate(160%) blur(10px);
      -webkit-backdrop-filter: saturate(160%) blur(10px);
      border-bottom: 1px solid var(--border);
      position: sticky;
      top: 0;
      z-index: 30;
    }
    /* Just the conv title in the topbar — prompt + cursor live at the
       bottom where the actual input is. */
    .topbar h1 {
      font-family: var(--font-display);
      font-size: 22px;
      font-weight: 400;
      letter-spacing: 0.02em;
      margin: 0;
      color: var(--accent);
      flex: 1;
      text-align: center;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      padding: 0 12px;
      cursor: pointer;
      line-height: 1;
      position: relative;
      text-shadow: 0 0 8px var(--accent-glow);
    }
    @keyframes phrac-blink { 50% { opacity: 0; } }
    .topbar h1:hover { background: var(--panel); }
    .topbar h1:focus-visible { outline: 1px solid var(--accent); outline-offset: 2px; }
    /* Small pencil glyph next to the title on hover — tells the user
       the title is editable, without adding a permanent button. */
    .topbar h1::after {
      content: "";
      display: inline-block;
      width: 12px;
      height: 12px;
      margin-left: 6px;
      vertical-align: -1px;
      background-color: currentColor;
      opacity: 0;
      transition: opacity 0.12s;
      mask: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><path d='M12 20h9'/><path d='M16.5 3.5a2.12 2.12 0 0 1 3 3L7 19l-4 1 1-4Z'/></svg>") no-repeat center / contain;
      -webkit-mask: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><path d='M12 20h9'/><path d='M16.5 3.5a2.12 2.12 0 0 1 3 3L7 19l-4 1 1-4Z'/></svg>") no-repeat center / contain;
    }
    .topbar h1:hover::after { opacity: 0.6; }
    .topbar input.title-edit {
      flex: 1;
      background: var(--panel);
      color: var(--text);
      border: 1px solid var(--accent);
      border-radius: 2px;
      padding: 4px 10px;
      font: inherit;
      font-size: 14px;
      text-align: center;
      outline: none;
      margin: 0 12px;
    }
    .topbar .burger {
      background: transparent;
      color: var(--text);
      border: 0;
      padding: 6px;
      cursor: pointer;
      border-radius: 2px;
      display: none;  /* shown at narrow widths */
    }
    .topbar .burger:hover { background: var(--panel); }
    .topbar .top-icon {
      background: transparent;
      color: var(--text-muted);
      border: 1px solid transparent;
      padding: 6px;
      border-radius: 2px;
      cursor: pointer;
      display: inline-flex;
      align-items: center;
      justify-content: center;
    }
    .topbar .top-icon:hover { background: var(--panel); color: var(--text); }
    .topbar .top-icon svg { width: 18px; height: 18px; }

    .user-menu { position: relative; }
    .user-pill {
      display: inline-flex;
      align-items: center;
      gap: 8px;
      background: transparent;
      border: 1px solid transparent;
      color: var(--text);
      padding: 4px 10px 4px 4px;
      border-radius: 2px;
      cursor: pointer;
      font: inherit;
    }
    .user-pill:hover { background: var(--panel); }
    .user-pill .avatar {
      width: 28px;
      height: 28px;
      border-radius: 50%;
      background: linear-gradient(135deg, #c97d4a 0%, #8b5a3a 100%);
      color: #fff;
      font-size: 12px;
      font-weight: 600;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      text-transform: uppercase;
    }
    .user-pill .username {
      font-size: 13px;
      max-width: 120px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .user-pill .owner-dot {
      width: 6px;
      height: 6px;
      border-radius: 50%;
      background: var(--accent);
      display: none;
    }
    .user-pill.is-owner .owner-dot { display: inline-block; }
    .user-menu-pop {
      position: absolute;
      top: calc(100% + 6px);
      right: 0;
      min-width: 180px;
      background: var(--panel);
      border: 1px solid var(--border);
      border-radius: 2px;
      padding: 6px;
      box-shadow: 0 8px 24px rgba(0,0,0,0.35);
      z-index: 40;
    }
    .user-menu-pop[hidden] { display: none; }
    .user-menu-pop .item {
      display: flex;
      align-items: center;
      gap: 10px;
      padding: 8px 10px;
      border-radius: 2px;
      color: var(--text);
      cursor: pointer;
      background: transparent;
      border: 0;
      width: 100%;
      text-align: left;
      font: inherit;
      text-decoration: none;
    }
    .user-menu-pop .item:hover { background: var(--panel-hover); }
    .user-menu-pop .item.muted { color: var(--text-muted); }
    .user-menu-pop hr {
      border: 0;
      border-top: 1px solid var(--border);
      margin: 4px 0;
    }

    /* Owner-only "flag this conversation for review" control. The button
       lives in the topbar and shows a hollow flag by default; when the
       active conversation is flagged the icon fills in and tints with
       the accent color so the state is glanceable. */
    .flag-menu { position: relative; display: inline-flex; }
    .flag-btn.is-flagged { color: var(--accent); }
    .flag-btn.is-flagged svg { fill: var(--accent); fill-opacity: 0.18; }
    .flag-pop {
      position: absolute;
      top: calc(100% + 6px);
      right: 0;
      width: 320px;
      background: var(--panel);
      border: 1px solid var(--border);
      border-radius: 2px;
      padding: 12px;
      box-shadow: 0 8px 24px rgba(0,0,0,0.35);
      z-index: 40;
      display: flex;
      flex-direction: column;
      gap: 8px;
    }
    .flag-pop[hidden] { display: none; }
    .flag-pop-title { font-size: 13px; font-weight: 600; color: var(--text); }
    .flag-pop-help { font-size: 12px; color: var(--text-muted); line-height: 1.4; }
    .flag-pop textarea {
      width: 100%;
      min-height: 84px;
      resize: vertical;
      background: var(--bg);
      color: var(--text);
      border: 1px solid var(--border);
      border-radius: 2px;
      padding: 8px 10px;
      font: inherit;
      font-size: 13px;
      outline: none;
      box-sizing: border-box;
    }
    .flag-pop textarea:focus { border-color: var(--accent); }
    .flag-pop-meta { font-size: 11px; color: var(--text-muted); }
    .flag-pop-actions {
      display: flex;
      align-items: center;
      gap: 6px;
    }
    .flag-spacer { flex: 1; }
    .flag-pop button {
      background: transparent;
      color: var(--text);
      border: 1px solid var(--border);
      border-radius: 2px;
      padding: 5px 10px;
      font: inherit;
      font-size: 12px;
      cursor: pointer;
    }
    .flag-pop button:hover:not(:disabled) { background: var(--panel-hover); }
    .flag-pop button:disabled { opacity: 0.45; cursor: not-allowed; }
    .flag-pop .flag-save {
      background: var(--accent);
      border-color: var(--accent);
      color: #fff;
    }
    .flag-pop .flag-save:hover:not(:disabled) { filter: brightness(1.08); }
    .flag-pop .flag-clear { color: #d8736b; border-color: rgba(216, 115, 107, 0.4); }
    .flag-pop-error {
      font-size: 12px;
      color: #d8736b;
    }

    #log {
      flex: 1;
      overflow-y: auto;
      padding: 24px 16px 140px;
      scrollbar-width: thin;
      scrollbar-color: #2a2218 transparent;
    }
    #log::-webkit-scrollbar { width: 10px; background: transparent; }
    #log::-webkit-scrollbar-thumb {
      background: #2a2218;
      border-radius: 2px;
      border: 2px solid var(--bg);  /* creates the inset-thumb look */
    }
    #log::-webkit-scrollbar-thumb:hover { background: #3d3120; }
    .conv-list { scrollbar-width: thin; scrollbar-color: #2a2218 transparent; }
    .conv-list::-webkit-scrollbar { width: 8px; background: transparent; }
    .conv-list::-webkit-scrollbar-thumb {
      background: #2a2218;
      border-radius: 2px;
      border: 2px solid var(--sidebar);
    }
    .log-inner {
      max-width: 760px;
      margin: 0 auto;
      display: flex;
      flex-direction: column;
      gap: 28px;
    }
    /* Boot-screen welcome — formatted as a terminal MOTD: ASCII banner,
       status lines, then a numbered command list. Click a row to either
       prefill the input or fire the command. */
    .welcome {
      text-align: left;
      color: var(--text);
      padding: 36px 8px 24px;
      font-family: var(--font-mono);
      font-size: 14px;
      line-height: 1.55;
      max-width: 640px;
      margin: 0 auto;
    }
    .welcome .banner {
      font-family: var(--font-display);
      font-size: 38px;
      line-height: 1.05;
      color: var(--accent);
      text-shadow: 0 0 14px var(--accent-glow);
      margin: 0 0 4px;
    }
    .welcome h2 {
      font-family: var(--font-display);
      color: var(--accent);
      margin: 0 0 12px;
      font-size: 22px;
      font-weight: 400;
      letter-spacing: 0.02em;
      text-shadow: 0 0 14px var(--accent-glow);
    }
    .welcome .status {
      color: var(--text-muted);
      margin: 0 0 16px;
      white-space: pre-wrap;
    }
    .welcome .status .ok { color: var(--accent); }
    .welcome p { margin: 0 0 12px; font-size: 14px; color: var(--text-muted); }
    .welcome-grid {
      display: flex;
      flex-direction: column;
      gap: 0;
      margin: 0;
    }
    .welcome-chip {
      background: transparent;
      border: 0;
      border-radius: 0;
      padding: 4px 0 4px 22px;
      color: var(--text-muted);
      font: inherit;
      font-family: var(--font-mono);
      font-size: 14px;
      text-align: left;
      cursor: pointer;
      position: relative;
      transition: color 0.08s;
    }
    .welcome-chip::before {
      content: counter(welcome-n) ")";
      counter-increment: welcome-n;
      position: absolute;
      left: 0;
      color: var(--accent);
      width: 18px;
    }
    .welcome-grid { counter-reset: welcome-n; }
    .welcome-chip:hover { color: var(--text); background: rgba(255,176,0,0.06); }
    .welcome-chip:active { color: var(--accent); }
    /* Inside the chip: bold label first, then the command body in
       a dimmer color (rendered with " — " separator from JS). */
    .welcome-chip .label {
      display: inline;
      color: var(--text);
      font-size: 14px;
      text-transform: none;
      letter-spacing: 0;
      font-weight: 500;
      margin-right: 6px;
    }
    .welcome-chip .label::after { content: " — "; color: var(--text-muted); font-weight: 400; }

    /* Jump-to-latest — sharp mono link, no rounded chip. */
    .jump-pill {
      position: fixed;
      right: 28px;
      bottom: 110px;
      z-index: 15;
      background: transparent;
      color: var(--accent);
      border: 0;
      padding: 4px 0;
      font-family: var(--font-mono);
      font-size: 12px;
      letter-spacing: 0.04em;
      display: none;
      align-items: center;
      gap: 4px;
      cursor: pointer;
      text-shadow: 0 0 6px var(--accent-glow);
      transition: opacity 0.12s;
    }
    .jump-pill::before { content: "[ "; color: var(--text-muted); }
    .jump-pill::after { content: " ]"; color: var(--text-muted); }
    .jump-pill:hover { opacity: 0.85; }
    .jump-pill.visible { display: inline-flex; }
    .jump-pill svg { width: 12px; height: 12px; }

    /* ---------- Messages ---------- */
    /* Operator-console message rows: no bubbles, no avatars. Each turn
       is a flush-left log row prefixed by a glyph that identifies who
       spoke — `>` for the user, `λ` for phracbot. The chevron sits in
       a fixed-width gutter so wrapped lines align under the body. */
    .msg {
      display: grid;
      grid-template-columns: 1.6em 1fr;
      gap: 8px;
      padding: 4px 0;
      align-items: baseline;
    }
    .msg > .body > *:first-child { margin-top: 0; }
    .msg > .body > *:last-child { margin-bottom: 0; }
    .msg::before {
      font-family: var(--font-mono);
      font-size: 14px;
      line-height: 1.6;
      color: var(--text-muted);
      user-select: none;
      grid-column: 1;
    }
    .msg.user::before { content: ">"; color: var(--accent); }
    .msg.bot::before { content: "λ"; color: var(--accent); text-shadow: 0 0 6px var(--accent-glow); }

    .msg.user .body,
    .msg.bot .body {
      font-family: var(--font-mono);
      font-size: 14px;
      line-height: 1.55;
      background: transparent;
      padding: 0;
      border: 0;
      border-radius: 0;
      max-width: 100%;
      min-width: 0;
      color: var(--text);
      grid-column: 2;
    }
    .msg.user .body { color: var(--text); }
    .msg.bot .body { color: var(--text); }

    /* Bot message bubbles: tighten inline-element sizing for chat context
       vs. an article page (original spec was too page-like). */
    .msg.bot h1, .msg.bot h2, .msg.bot h3, .msg.bot h4 {
      font-family: var(--font-mono);
      font-weight: 600;
      color: var(--accent);
      letter-spacing: 0.01em;
    }
    .msg.bot h1 { font-size: 17px; margin: 14px 0 6px; }
    .msg.bot h2 { font-size: 15px; margin: 12px 0 5px; }
    .msg.bot h3 { font-size: 14px; margin: 10px 0 4px; }
    .msg.bot h4 { font-size: 14px; margin: 10px 0 4px; }
    .msg p { margin: 8px 0; }
    .msg ul, .msg ol { margin: 8px 0; }
    .msg li { margin: 2px 0; }
    /* Wrap tables so wide ones scroll horizontally inside the bubble
       instead of overflowing the chat column. */
    .table-scroll {
      overflow-x: auto;
      -webkit-overflow-scrolling: touch;
      margin: 10px 0;
    }
    .msg table { margin: 0; }
    /* Long URLs in links shouldn't push the bubble off-screen. */
    .msg a { word-break: break-word; }

    .msg pre, .msg code {
      font-family: "Geist Mono", "SF Mono", Consolas, Menlo, ui-monospace, monospace;
      font-size: 13.5px;
    }
    .msg pre {
      background: #18181b;
      padding: 14px 16px;
      padding-right: 60px;           /* reserve room so the copy button never overlaps content */
      border-radius: 2px;
      overflow-x: auto;
      overflow-y: hidden;
      border: 1px solid var(--border);
      position: relative;
      margin: 12px 0;
      line-height: 1.55;
    }
    /* Inline code — subtle pill on panel background. */
    .msg code {
      background: rgba(255, 255, 255, 0.07);
      padding: 1.5px 6px;
      border-radius: 2px;
      color: #f0f0f0;
    }
    .msg pre code {
      background: transparent;
      padding: 0;
      color: #e6e6e6;
      white-space: pre;
    }
    /* Themed horizontal scrollbar inside code blocks — thin, dark, matches border. */
    .msg pre::-webkit-scrollbar { height: 8px; background: transparent; }
    .msg pre::-webkit-scrollbar-track { background: transparent; }
    .msg pre::-webkit-scrollbar-thumb {
      background: #2a2218;
      border-radius: 2px;
    }
    .msg pre::-webkit-scrollbar-thumb:hover { background: #3d3120; }
    /* Firefox */
    .msg pre { scrollbar-width: thin; scrollbar-color: #2a2218 transparent; }
    .copy-btn {
      position: absolute;
      top: 8px;
      right: 8px;
      background: rgba(255, 255, 255, 0.04);
      color: var(--text-muted);
      border: 1px solid var(--border);
      border-radius: 2px;
      font-size: 11px;
      font-family: inherit;
      padding: 4px 9px;
      cursor: pointer;
      opacity: 0;
      transition: opacity 0.15s, background 0.15s, color 0.15s;
      letter-spacing: 0.02em;
    }
    .msg pre:hover .copy-btn { opacity: 1; }
    .copy-btn:hover { color: var(--text); background: rgba(255, 255, 255, 0.08); }
    .copy-btn.copied { color: var(--accent); border-color: var(--accent); }

    .msg table { border-collapse: collapse; margin: 10px 0; font-size: 14px; }
    .msg th, .msg td { border: 1px solid var(--border); padding: 6px 12px; text-align: left; }
    .msg a { color: var(--accent); text-decoration: none; }
    .msg a:hover { text-decoration: underline; }
    .msg h1, .msg h2, .msg h3, .msg h4 { margin: 14px 0 6px; line-height: 1.3; }
    .msg h1 { font-size: 22px; }
    .msg h2 { font-size: 19px; }
    .msg h3 { font-size: 17px; }
    .msg ul, .msg ol { padding-left: 24px; }
    .msg blockquote {
      border-left: 3px solid var(--border);
      padding-left: 12px;
      color: var(--text-muted);
      margin: 10px 0;
    }

    .msg.system {
      display: block;
      color: var(--text-muted);
      font-family: var(--font-mono);
      font-style: normal;
      font-size: 12px;
      text-align: center;
      padding: 6px 20px;
      letter-spacing: 0.04em;
    }
    .msg.system::before { content: none; }
    .msg.error::before { content: none; }
    .msg.error {
      display: flex;
      align-items: flex-start;
      gap: 10px;
      background: rgba(231, 76, 60, 0.08);
      border: 1px solid rgba(231, 76, 60, 0.35);
      color: #f5b8b3;
      font-size: 14px;
      padding: 10px 14px;
      border-radius: 2px;
      margin: 6px 0;
    }
    .msg.error svg { flex-shrink: 0; margin-top: 2px; color: var(--danger); }
    .msg.error .err-body { flex: 1; white-space: pre-wrap; word-break: break-word; }
    .msg.error .err-close {
      background: transparent;
      border: 0;
      color: var(--text-muted);
      cursor: pointer;
      padding: 0 4px;
      font-size: 16px;
      line-height: 1;
    }
    .msg.error .err-close:hover { color: var(--text); }

    /* Per-message action bar (bot only). Always-visible but subtle;
       brightens on row hover. Icons match the rest of the UI. */
    .msg-actions {
      display: flex;
      gap: 2px;
      margin-top: 4px;
      margin-left: -6px;  /* nudge left so the first icon aligns with body */
      opacity: 0.55;
      transition: opacity 0.15s ease;
    }
    .msg-usage {
      font-size: 11px;
      color: var(--text-muted);
      margin-top: 6px;
      font-variant-numeric: tabular-nums;
      opacity: 0.6;
    }
    .msg.bot:hover .msg-actions { opacity: 1; }
    .msg-action {
      background: transparent;
      border: 1px solid transparent;
      color: var(--text-muted);
      padding: 5px;
      border-radius: 2px;
      cursor: pointer;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      transition: background 0.1s, color 0.1s;
    }
    .msg-action:hover { background: var(--panel); color: var(--text); }
    .msg-action.active { color: var(--accent); }
    /* Thumbs-down reads negative — paint it red when active so the
       state is glanceable distinct from thumbs-up. */
    .msg-action[data-action="thumbdown"].active { color: #d8736b; }

    /* Hover timestamp — uses the [data-ts] attribute on .msg.
       Uses ::after because ::before is the prefix glyph. */
    .msg[data-ts] { position: relative; }
    .msg[data-ts]::after {
      content: attr(data-ts);
      position: absolute;
      top: -16px;
      left: 30px;
      font-family: var(--font-mono);
      font-size: 10px;
      letter-spacing: 0.04em;
      color: var(--text-muted);
      background: var(--bg);
      padding: 1px 6px;
      opacity: 0;
      pointer-events: none;
      transition: opacity 0.12s;
      white-space: nowrap;
    }
    .msg[data-ts]:hover::after { opacity: 1; }

    /* Edit affordance on user bubbles. Lives next to the bubble, only
       visible on hover + only on the last user message (marker class
       applied in renderLog). */
    .msg.user .edit-row {
      display: flex;
      justify-content: flex-end;
      gap: 4px;
      margin-top: 4px;
      opacity: 0;
      transition: opacity 0.15s;
    }
    .msg.user.editable:hover .edit-row { opacity: 1; }
    .msg.user .edit-btn {
      background: transparent;
      border: 1px solid transparent;
      color: var(--text-muted);
      padding: 4px 6px;
      border-radius: 2px;
      cursor: pointer;
      display: inline-flex;
      align-items: center;
    }
    .msg.user .edit-btn:hover { background: var(--panel); color: var(--text); }
    .msg.user .edit-btn svg { width: 14px; height: 14px; }

    /* Inline edit mode: textarea with Save/Cancel inside the user bubble. */
    .msg.user .edit-wrap { display: flex; flex-direction: column; gap: 8px; width: 520px; max-width: 100%; }
    .msg.user .edit-wrap textarea {
      background: rgba(0,0,0,0.25);
      color: var(--text);
      border: 1px solid #3d3120;
      border-radius: 2px;
      padding: 8px 10px;
      font: inherit;
      resize: vertical;
      min-height: 80px;
    }
    .msg.user .edit-wrap .edit-actions { display: flex; justify-content: flex-end; gap: 6px; }
    .msg.user .edit-wrap button {
      background: var(--panel);
      color: var(--text);
      border: 1px solid #3d3120;
      border-radius: 2px;
      padding: 6px 14px;
      font: inherit;
      cursor: pointer;
    }
    .msg.user .edit-wrap button.primary {
      background: var(--accent);
      color: #000;
      border-color: var(--accent);
    }
    .msg.user .edit-wrap button:hover { filter: brightness(1.1); }

    /* Send/stop button state */
    .send-btn.stopping {
      background: var(--danger);
      color: #fff;
    }
    .msg-action.flash { color: var(--accent); }
    .msg-action svg { width: 15px; height: 15px; }

    /* Typing indicator */
    .typing-wrap {
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      gap: 2px;
      padding: 2px 0;
    }
    .tool-status {
      color: var(--text-muted);
      font-size: 13px;
      font-family: "Geist Mono", "SF Mono", Consolas, Menlo, ui-monospace, monospace;
      min-height: 18px;
    }
    /* Hide the bouncing dots once streamed content has arrived — the
       growing text is its own liveness signal. `:has()` is widely
       supported in modern browsers now. */
    .typing-row .body:has(.streaming-content:not(:empty)) .typing { display: none; }
    /* Also hide the dots while reasoning is streaming — the reasoning
       block's "Reasoning" chip + growing body is enough of a liveness
       signal. */
    .typing-row .body:has(.reasoning-block:not([hidden])) .typing { display: none; }

    /* Reasoning panel — shown above the main response when the
       per-conv thinking toggle is on. Collapsed by default; click the
       summary to expand. Muted styling so it doesn't compete with the
       answer. */
    .reasoning-block {
      margin-bottom: 10px;
      border: 1px solid var(--border);
      border-radius: 2px;
      background: rgba(255, 255, 255, 0.02);
      font-size: 13px;
    }
    .reasoning-block > summary {
      cursor: pointer;
      padding: 6px 10px;
      color: var(--text-muted);
      font-family: "Geist Mono", "SF Mono", Consolas, Menlo, ui-monospace, monospace;
      user-select: none;
      list-style: none;
    }
    .reasoning-block > summary::-webkit-details-marker { display: none; }
    .reasoning-block > summary::before {
      content: "▸";
      display: inline-block;
      width: 14px;
      transition: transform 120ms ease-out;
    }
    .reasoning-block[open] > summary::before {
      transform: rotate(90deg);
    }
    .reasoning-block > summary:hover {
      color: var(--text);
      background: rgba(255, 255, 255, 0.03);
      border-radius: 2px 2px 0 0;
    }
    .reasoning-block[open] > summary {
      border-bottom: 1px solid var(--border);
    }
    .reasoning-block .reasoning-body {
      padding: 8px 12px 10px 12px;
      color: var(--text-muted);
      white-space: pre-wrap;
      word-wrap: break-word;
      font-family: "Geist Mono", "SF Mono", Consolas, Menlo, ui-monospace, monospace;
      font-size: 12px;
      line-height: 1.5;
      max-height: 400px;
      overflow-y: auto;
    }
    .tool-status code {
      background: rgba(255, 255, 255, 0.07);
      padding: 1px 5px;
      border-radius: 2px;
      color: #e6e6e6;
    }
    .typing {
      display: inline-flex;
      gap: 4px;
      padding: 4px 0;
    }
    .typing span {
      width: 7px;
      height: 7px;
      background: var(--text-muted);
      border-radius: 50%;
      animation: blink 1.4s infinite both;
    }
    .typing span:nth-child(2) { animation-delay: 0.2s; }
    .typing span:nth-child(3) { animation-delay: 0.4s; }
    @keyframes blink {
      0%, 60%, 100% { opacity: 0.25; }
      30% { opacity: 1; }
    }

    /* ---------- Input ---------- */
    .input-wrap {
      position: absolute;
      bottom: 0;
      left: var(--sidebar-w);
      right: 0;
      padding: 22px 16px 18px;
      background: linear-gradient(to bottom, transparent, var(--bg) 35%);
      pointer-events: none;
    }
    .input-inner {
      max-width: 760px;
      margin: 0 auto;
      pointer-events: auto;
      position: relative;  /* anchor for the slash menu */
    }

    /* Slash-command autocomplete. Opens when textarea starts with
       CMD_PREFIX; filtered by the tokens after it. Floats above the
       input wrap. Keyboard-nav: ↑ / ↓ / Tab / Enter. Escape to dismiss. */
    .slash-menu {
      position: absolute;
      bottom: calc(100% + 8px);
      left: 0;
      right: 0;
      max-height: 320px;
      overflow-y: auto;
      background: var(--panel);
      border: 1px solid var(--border);
      border-radius: var(--radius);
      padding: 4px;
      box-shadow: 0 10px 32px rgba(0,0,0,0.45);
      z-index: 20;
      font-family: inherit;
    }
    .slash-menu[hidden] { display: none; }

    /* Conversation-settings popover: opens above the input row from the
       cog button next to attach. Compact so it doesn't obscure the
       chat — just the thinking toggle and a collapsible prompt editor. */
    .cog-pop {
      position: absolute;
      bottom: calc(100% + 8px);
      left: 8px;
      right: 8px;
      max-width: 520px;
      background: var(--panel);
      border: 1px solid var(--border);
      border-radius: var(--radius);
      padding: 12px 14px;
      box-shadow: 0 10px 32px rgba(0,0,0,0.45);
      z-index: 22;
      font-family: inherit;
    }
    .cog-pop[hidden] { display: none; }
    .cog-pop-head {
      display: flex; justify-content: space-between; align-items: baseline;
      margin-bottom: 10px; font-size: 13px; font-weight: 600;
    }
    .cog-pop-head .muted { color: var(--text-muted); font-weight: 400; font-size: 12px; }
    .cog-pop-row {
      display: flex; align-items: center; justify-content: space-between;
      gap: 12px; padding: 8px 0;
    }
    .cog-pop-meta { flex: 1; min-width: 0; }
    .cog-pop-meta .title { display: block; font-size: 13px; }
    .cog-pop-meta .desc { display: block; color: var(--text-muted); font-size: 11px; margin-top: 2px; }
    .cog-pop-section {
      margin-top: 10px;
      padding-top: 10px;
      border-top: 1px solid var(--border);
    }
    .cog-pop-label {
      display: block; font-size: 12px; font-weight: 500;
      color: var(--text-muted); margin-bottom: 6px;
    }
    .cog-pop textarea {
      width: 100%; resize: vertical; min-height: 90px;
      background: #13100b; color: var(--text);
      border: 1px solid var(--border); border-radius: 2px;
      padding: 8px 10px; font: inherit; font-size: 13px;
      font-family: "Geist Mono", monospace;
    }
    .cog-pop-actions {
      display: flex; justify-content: space-between; align-items: center;
      gap: 10px; margin-top: 8px;
    }
    .cog-pop-actions .status { font-size: 12px; margin: 0; }
    .cog-pop-actions .status.ok { color: var(--accent); }
    .cog-pop-actions .status.err { color: var(--danger); }
    .slash-item {
      display: grid;
      grid-template-columns: 180px 1fr;
      gap: 12px;
      align-items: baseline;
      padding: 9px 12px;
      border-radius: var(--radius-sm);
      cursor: pointer;
      color: var(--text);
    }
    .slash-item:hover { background: var(--panel-hover); }
    .slash-item.active { background: var(--panel-hover); outline: 1px solid #444; }
    .slash-item .name {
      font-family: inherit;
      font-size: 14px;
      font-weight: 500;
      color: var(--text);
    }
    .slash-item .desc {
      font-family: inherit;
      color: var(--text-muted);
      font-size: 13px;
      line-height: 1.4;
    }
    .slash-menu::-webkit-scrollbar { width: 8px; background: transparent; }
    .slash-menu::-webkit-scrollbar-thumb {
      background: #2a2218;
      border-radius: 2px;
      border: 2px solid var(--panel);
    }
    /* Terminal-style input: no box, no border, no shadow. ps1 prefix
       sits inline with the textarea; the textarea's own caret is the
       cursor. Empty state shows a blinking ▍ via the placeholder. */
    .chat-form {
      background: transparent;
      border: 0;
      border-radius: 0;
      padding: 0;
      display: flex;
      align-items: flex-start;
      gap: 8px;
      box-shadow: none;
      font-family: var(--font-mono);
      font-size: 14px;
      line-height: 1.6;
    }
    .chat-form .ps1 {
      color: var(--accent);
      text-shadow: 0 0 6px var(--accent-glow);
      flex-shrink: 0;
      padding-top: 1px;     /* baseline-align with the textarea text */
      user-select: none;
      white-space: nowrap;
    }
    textarea {
      flex: 1;
      background: transparent;
      color: var(--text);
      border: 0;
      outline: 0;
      font-family: var(--font-mono);
      font-size: 14px;
      resize: none;
      line-height: 1.6;
      padding: 0;
      max-height: 220px;
      caret-color: var(--accent);
    }
    textarea::placeholder {
      color: var(--accent);
      text-shadow: 0 0 6px var(--accent-glow);
      animation: phrac-blink 1.05s steps(1) infinite;
      opacity: 1;
    }
    /* ASCII-bracket icon buttons trailing the input. Dim by default,
       brighten on hover. Send button is hidden — Enter submits. */
    .form-icon {
      background: transparent;
      border: 0;
      padding: 0 4px;
      color: var(--text-muted);
      font-family: var(--font-mono);
      font-size: 13px;
      line-height: 1.6;
      cursor: pointer;
      flex-shrink: 0;
      transition: color 0.1s;
    }
    .form-icon:hover { color: var(--accent); }
    .send-btn { display: none; }
    .send-btn, .attach-btn { /* legacy guard; kept so JS hooks don't 404 */ }
    .mic-btn {
      background: transparent;
      color: var(--text-muted);
      border: 1px solid transparent;
    }
    .mic-btn:hover { color: var(--text); background: rgba(255,255,255,0.05); }
    .mic-btn.recording {
      color: #ef4444;
      background: rgba(239,68,68,0.12);
      animation: mic-pulse 1.2s ease-in-out infinite;
    }
    @keyframes mic-pulse {
      0%, 100% { box-shadow: 0 0 0 0 rgba(239,68,68,0.5); }
      50% { box-shadow: 0 0 0 6px rgba(239,68,68,0); }
    }

    /* File chip shown above input while upload is pending/in-flight */
    .file-chip {
      background: var(--panel);
      border: 1px solid var(--border);
      border-radius: 2px;
      padding: 8px 10px;
      display: flex;
      align-items: center;
      gap: 10px;
      font-size: 13px;
      margin-bottom: 8px;
    }
    .file-chip .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .file-chip .state { color: var(--text-muted); font-size: 12px; }
    .file-chip .close {
      background: transparent; color: var(--text-muted); border: 0;
      cursor: pointer; padding: 2px 6px; border-radius: 2px;
    }
    .file-chip .close:hover { color: var(--danger); }
    .file-chip img.thumb {
      width: 40px;
      height: 40px;
      object-fit: cover;
      border-radius: 2px;
      flex-shrink: 0;
    }

    /* Attachment shown inside a user message bubble */
    .msg-attach {
      display: flex;
      gap: 8px;
      align-items: center;
      background: rgba(0,0,0,0.2);
      border: 1px solid var(--border);
      border-radius: 2px;
      padding: 6px 10px;
      margin-bottom: 8px;
      font-size: 13px;
      max-width: 320px;
    }
    .msg-attach .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .msg-attach .size { color: var(--text-muted); font-size: 11px; }
    img.thumb-lg {
      max-width: 420px;
      max-height: 320px;
      width: auto;
      height: auto;
      border-radius: 2px;
      display: block;
      margin-bottom: 8px;
      cursor: zoom-in;
      border: 1px solid var(--border);
      transition: transform 0.1s ease;
    }
    img.thumb-lg:hover { transform: scale(1.01); border-color: #3d3120; }
    .msg.user .msg-attach { background: rgba(0,0,0,0.25); }

    /* ---------- Inline citation chips ---------- */
    /* Promote `[verified]` / `[unverified]` tags the model emits at
       sentence end into compact visual markers so a tech can scan a
       reply and see what's grounded vs. inferred. Hover for the tag's
       meaning. Colors are subtle on purpose — chips should help, not
       compete with the answer. */
    .cite {
      display: inline-block;
      vertical-align: baseline;
      margin: 0 1px 0 3px;
      padding: 0 5px;
      border-radius: 2px;
      font: 600 10.5px -apple-system, "Segoe UI", sans-serif;
      line-height: 14px;
      cursor: help;
      user-select: none;
      font-feature-settings: "tnum";
    }
    .cite-v {
      background: rgba(74, 139, 95, 0.22);
      color: #88c79a;
      border: 1px solid rgba(74, 139, 95, 0.5);
    }
    .cite-unv {
      background: rgba(194, 144, 74, 0.22);
      color: #d4a86a;
      border: 1px solid rgba(194, 144, 74, 0.5);
    }
    .cite-v:hover  { background: rgba(74, 139, 95, 0.34); }
    .cite-unv:hover { background: rgba(194, 144, 74, 0.34); }

    /* ---------- Image-search thumbnail grid ---------- */
    /* Compact horizontal row of result thumbnails with numbered badges.
       Used by image_search; the LLM references results by their badge
       number ("see #2") so the user knows which inline image is which. */
    .thumb-grid {
      display: flex;
      flex-wrap: wrap;
      gap: 8px;
      margin: 6px 0 10px;
    }
    .thumb-grid-item {
      position: relative;
      width: 120px;
      height: 120px;
      border-radius: 2px;
      overflow: hidden;
      border: 1px solid var(--border);
      background: rgba(255, 255, 255, 0.03);
      cursor: zoom-in;
      transition: transform 0.1s ease, border-color 0.1s ease;
    }
    .thumb-grid-item:hover { transform: scale(1.02); border-color: #3d3120; }
    .thumb-grid-item img {
      width: 100%;
      height: 100%;
      object-fit: cover;
      display: block;
    }
    .thumb-grid-item .badge {
      position: absolute;
      top: 4px;
      left: 4px;
      background: rgba(0, 0, 0, 0.72);
      color: #fff;
      font-size: 11px;
      font-weight: 600;
      padding: 2px 6px;
      border-radius: 2px;
      pointer-events: none;
      font-feature-settings: "tnum";
    }
    @media (max-width: 600px) {
      .thumb-grid-item { width: 96px; height: 96px; }
    }

    /* Lightbox for clicked images */
    #lightbox {
      position: fixed;
      inset: 0;
      background: rgba(0, 0, 0, 0.85);
      display: none;
      align-items: center;
      justify-content: center;
      z-index: 200;
      padding: 24px;
      backdrop-filter: blur(4px);
    }
    #lightbox.open { display: flex; }
    #lightbox img {
      max-width: 100%;
      max-height: 100%;
      border-radius: 2px;
      box-shadow: 0 10px 40px rgba(0,0,0,0.6);
      cursor: zoom-out;
    }
    #lightbox .lb-close {
      position: absolute;
      top: 16px;
      right: 16px;
      background: rgba(255,255,255,0.1);
      color: #fff;
      border: 0;
      width: 36px;
      height: 36px;
      border-radius: 50%;
      font-size: 20px;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    #lightbox .lb-close:hover { background: rgba(255,255,255,0.2); }

    /* Drag-drop overlay */
    #dropOverlay {
      position: fixed;
      inset: 0;
      background: rgba(255, 176, 0, 0.1);
      border: 3px dashed var(--accent);
      display: none;
      align-items: center;
      justify-content: center;
      z-index: 80;
      pointer-events: none;
      font-size: 20px;
      color: var(--accent);
      font-weight: 600;
    }
    #dropOverlay.active { display: flex; }
    .tiny-hint {
      text-align: center;
      color: var(--text-muted);
      font-size: 11px;
      margin-top: 8px;
    }

    /* ---------- Context-window usage bar ---------- */
    /* Lives between the chat form and the tiny-hint. Slim track + fill
       that shifts color as the prompt fills the model's context window.
       `hidden` until the first response carries a usage figure — first
       turn of a conv has nothing to show yet. */
    /* Context-window meter: an ASCII block bar styled as a terminal
       status line. The track is "░░░…" and the fill is "███…" of the
       same length, layered so the fill overdraws the leftmost cells. */
    .ctx-bar {
      margin-top: 6px;
      display: flex;
      align-items: baseline;
      gap: 8px;
      user-select: none;
      font-family: var(--font-mono);
      font-size: 11px;
      letter-spacing: 0;
      color: var(--text-muted);
      line-height: 1.3;
    }
    .ctx-bar[hidden] { display: none; }
    .ctx-bar::before {
      content: "ctx";
      color: var(--text-muted);
      flex-shrink: 0;
      letter-spacing: 0.05em;
    }
    .ctx-bar-track {
      flex-shrink: 0;
      letter-spacing: 0;
    }
    .ctx-bar-track::before { content: "["; color: var(--text-muted); margin-right: 1px; }
    .ctx-bar-track::after  { content: "]"; color: var(--text-muted); margin-left: 1px; }
    .ctx-bar-empty {
      color: rgba(255, 176, 0, 0.18);
    }
    .ctx-bar-fill {
      color: var(--accent);
      text-shadow: 0 0 4px var(--accent-glow);
      transition: color 240ms ease;
    }
    .ctx-bar-fill.warn  { color: #ff7a1a; text-shadow: 0 0 4px rgba(255, 122, 26, 0.45); }
    .ctx-bar-fill.high  { color: var(--danger); text-shadow: 0 0 4px rgba(239, 83, 80, 0.45); }
    .ctx-bar-label {
      display: flex;
      gap: 8px;
      color: var(--text-muted);
      font-feature-settings: "tnum";
      flex-shrink: 0;
    }
    .ctx-bar-pct { color: var(--accent); }
    .ctx-bar-tokens::before { content: "· "; color: var(--text-muted); }

    /* ---------- Modal (token setup) ---------- */
    #setup {
      position: fixed;
      inset: 0;
      background: rgba(10, 10, 10, 0.7);
      display: none;
      align-items: center;
      justify-content: center;
      z-index: 100;
      backdrop-filter: blur(3px);
    }
    #setup.open { display: flex; }
    #setup form {
      background: var(--panel);
      border: 1px solid var(--border);
      border-radius: var(--radius);
      padding: 22px;
      display: flex;
      flex-direction: column;
      width: 380px;
      max-width: 90vw;
      gap: 12px;
    }
    #setup h2 { margin: 0; font-size: 17px; }
    #setup p { color: var(--text-muted); margin: 0 0 6px; font-size: 13px; }
    #setup input {
      background: #13100b;
      color: var(--text);
      border: 1px solid var(--border);
      border-radius: 2px;
      padding: 10px;
      font: inherit;
      font-size: 14px;
    }
    #setup button {
      background: var(--accent);
      color: #fff;
      border: 0;
      border-radius: 2px;
      padding: 10px;
      font: inherit;
      font-size: 14px;
      font-weight: 600;
      cursor: pointer;
    }
    .auth-error {
      color: var(--danger);
      font-size: 13px;
      margin: 0 0 4px;
      min-height: 16px;
    }
    .auth-error:empty { display: none; }

    /* Settings — two-column modal, Claude/ChatGPT style. Left nav
       selects a section, right column shows its controls.

       Font sizing deliberately mirrors the chat column: 15px body,
       14px secondary, so the modal feels like part of the same app
       rather than a system dialog tacked on. */
    #settings {
      position: fixed;
      inset: 0;
      background: rgba(10,10,10,0.6);
      display: none;
      align-items: center;
      justify-content: center;
      z-index: 110;
      backdrop-filter: blur(4px);
      font-family: inherit;
    }
    #settings.open { display: flex; }
    .settings-panel {
      background: var(--panel);
      border: 1px solid var(--border);
      border-radius: 2px;
      width: 720px;
      max-width: 94vw;
      height: 580px;
      max-height: 88vh;
      display: flex;
      flex-direction: column;
      overflow: hidden;
      box-shadow: 0 24px 60px rgba(0,0,0,0.5);
      font-family: inherit;
    }
    .settings-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 16px 20px 14px;
      border-bottom: 1px solid var(--border);
    }
    .settings-header h2 {
      margin: 0;
      font-family: var(--font-display);
      font-size: 24px;
      font-weight: 400;
      letter-spacing: 0.02em;
      color: var(--accent);
      text-shadow: 0 0 8px var(--accent-glow);
    }
    .settings-close {
      background: transparent;
      border: 0;
      color: var(--text-muted);
      cursor: pointer;
      padding: 4px 10px;
      border-radius: 2px;
      font: inherit;
      font-size: 22px;
      line-height: 1;
    }
    .settings-close:hover { background: var(--panel-hover); color: var(--text); }

    .settings-cols {
      flex: 1;
      display: grid;
      grid-template-columns: 192px 1fr;
      min-height: 0;
    }
    .settings-nav {
      border-right: 1px solid var(--border);
      padding: 14px 8px;
      display: flex;
      flex-direction: column;
      gap: 2px;
      overflow-y: auto;
    }
    .settings-nav button {
      background: transparent;
      border: 0;
      border-radius: 2px;
      padding: 10px 12px;
      text-align: left;
      color: var(--text);
      font: inherit;
      font-size: 14px;
      cursor: pointer;
      display: flex;
      align-items: center;
      gap: 10px;
    }
    .settings-nav button svg { width: 16px; height: 16px; color: var(--text-muted); }
    .settings-nav button:hover { background: var(--panel-hover); }
    .settings-nav button.active {
      background: var(--panel-hover);
      font-weight: 500;
    }
    .settings-nav button.active svg { color: var(--text); }

    .settings-body {
      padding: 24px 28px;
      overflow-y: auto;
      color: var(--text);
      font-size: 16px;
      line-height: 1.55;
    }
    .settings-pane { display: none; }
    .settings-pane.active { display: block; }
    .settings-pane h3 {
      margin: 0 0 4px;
      font-size: 17px;
      font-weight: 600;
      letter-spacing: -0.005em;
      font-family: inherit;
    }
    .settings-pane .pane-hint {
      color: var(--text-muted);
      font-size: 14px;
      margin: 0 0 22px;
      line-height: 1.5;
    }
    .settings-subhead {
      font-size: 16px;
      font-weight: 600;
      margin: 0 0 4px;
    }
    .settings-field {
      display: flex;
      flex-direction: column;
      gap: 6px;
      margin-bottom: 14px;
    }
    .settings-field label {
      font-size: 12px;
      font-weight: 500;
      color: var(--text-muted);
      text-transform: uppercase;
      letter-spacing: 0.05em;
    }
    .settings-field .value {
      font-size: 16px;
      color: var(--text);
    }
    .settings-field input[type="text"],
    .settings-field input[type="password"] {
      background: #13100b;
      color: var(--text);
      border: 1px solid var(--border);
      border-radius: 2px;
      padding: 10px 12px;
      font: inherit;
      font-size: 15px;
      transition: border-color 0.12s;
    }
    .settings-field input:focus {
      outline: none;
      border-color: #666;
    }
    .settings-field input::placeholder { color: #6a6a6a; }
    .settings-divider {
      border: 0;
      border-top: 1px solid var(--border);
      margin: 24px 0 20px;
    }

    .settings-form {
      display: flex;
      flex-direction: column;
    }
    .settings-form .row-actions {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-top: 8px;
      gap: 12px;
    }
    .settings-form .status {
      font-size: 13px;
      color: var(--text-muted);
      margin: 0;
      flex: 1;
    }
    .settings-form .status.ok { color: var(--accent); }
    .settings-form .status.err { color: var(--danger); }
    .btn-primary {
      background: var(--accent);
      color: #0a0a0a;
      border: 0;
      border-radius: 2px;
      padding: 9px 18px;
      font: inherit;
      font-size: 14px;
      font-weight: 600;
      cursor: pointer;
      transition: filter 0.12s;
    }
    .btn-primary:hover { filter: brightness(1.08); }
    .btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
    .btn-secondary {
      background: transparent;
      color: var(--text);
      border: 1px solid var(--border);
      border-radius: 2px;
      padding: 8px 16px;
      font: inherit;
      font-size: 14px;
      cursor: pointer;
      margin-right: 8px;
    }
    .btn-secondary:hover { background: var(--panel-hover); }

    /* Row-aligned controls (a label on the left, control on the right) */
    .settings-row {
      display: flex;
      align-items: flex-start;
      justify-content: space-between;
      padding: 16px 0;
      gap: 20px;
      border-top: 1px solid var(--border);
    }
    .settings-row:first-of-type { border-top: 0; padding-top: 0; }
    .settings-row .meta { display: flex; flex-direction: column; gap: 4px; max-width: 420px; }
    .settings-row .meta .title { font-size: 16px; font-weight: 500; }
    .settings-row .meta .desc { font-size: 14px; color: var(--text-muted); line-height: 1.5; }

    /* Toggle switch — iOS-style */
    .toggle {
      --w: 36px;
      --h: 20px;
      position: relative;
      width: var(--w);
      height: var(--h);
      display: inline-block;
      flex-shrink: 0;
    }
    .toggle input { opacity: 0; width: 0; height: 0; }
    .toggle .track {
      position: absolute; inset: 0;
      background: #444;
      border-radius: 2px;
      cursor: pointer;
      transition: background 0.15s;
    }
    .toggle .track::before {
      content: "";
      position: absolute;
      top: 2px; left: 2px;
      width: calc(var(--h) - 4px);
      height: calc(var(--h) - 4px);
      background: #fff;
      border-radius: 50%;
      transition: transform 0.15s;
    }
    .toggle input:checked + .track { background: var(--accent); }
    .toggle input:checked + .track::before { transform: translateX(calc(var(--w) - var(--h))); }

    /* Knowledge library modal — shared scrim pattern with settings. */
    #library {
      position: fixed;
      inset: 0;
      background: rgba(10,10,10,0.6);
      display: none;
      align-items: center;
      justify-content: center;
      z-index: 112;
      backdrop-filter: blur(4px);
    }
    #library.open { display: flex; }

    /* Note editor — same scrim, larger panel, textarea-first body.
       Opens via `/rag note` with no body, the "Write a note" button
       inside the Library modal, or the Edit button on a note row. */
    #note-modal {
      position: fixed; inset: 0;
      background: rgba(10,10,10,0.6);
      display: none; align-items: center; justify-content: center;
      z-index: 114; /* above Library so Edit-from-Library can stack */
      backdrop-filter: blur(4px);
    }
    #note-modal.open { display: flex; }
    .note-panel {
      background: var(--panel);
      border: 1px solid var(--border);
      border-radius: 2px;
      width: 820px;
      max-width: 96vw;
      height: 640px;
      max-height: 90vh;
      display: flex;
      flex-direction: column;
      overflow: hidden;
      box-shadow: 0 24px 60px rgba(0,0,0,0.5);
    }
    .note-body {
      flex: 1;
      display: flex;
      padding: 14px 20px;
      min-height: 0;
    }
    .note-body textarea {
      flex: 1;
      width: 100%;
      resize: none;
      background: #141414;
      color: var(--text);
      border: 1px solid var(--border);
      border-radius: 2px;
      padding: 12px 14px;
      font-family: "Geist Mono", "SF Mono", Consolas, Menlo, ui-monospace, monospace;
      font-size: 14px;
      line-height: 1.55;
      outline: none;
    }
    .note-body textarea:focus {
      border-color: var(--accent);
      box-shadow: 0 0 0 2px rgba(255, 120, 40, 0.12);
    }
    .note-foot {
      display: flex;
      align-items: center;
      justify-content: space-between;
      gap: 16px;
      padding: 12px 20px 16px;
      border-top: 1px solid var(--border);
    }
    .note-foot .note-scope-label {
      color: var(--text-muted);
      font-size: 13px;
    }
    .note-foot-right {
      display: flex;
      align-items: center;
      gap: 10px;
    }
    .note-foot .status {
      font-size: 12px;
      color: var(--text-muted);
    }
    .note-foot .status.ok { color: var(--accent); }
    .note-foot .status.err { color: var(--danger); }
    .note-btn {
      padding: 7px 14px;
      border-radius: 2px;
      border: 1px solid var(--border);
      background: #13100b;
      color: var(--text);
      cursor: pointer;
      font-size: 13px;
    }
    .note-btn:hover { background: var(--panel-hover); }
    .note-btn.primary {
      background: var(--accent);
      border-color: var(--accent);
      color: #0a0a0a;
      font-weight: 600;
    }
    .note-btn.primary:hover { filter: brightness(1.08); }
    .note-btn:disabled { opacity: 0.5; cursor: not-allowed; }

    /* "Write a note" CTA + per-row "Edit" in the library table. */
    .library-write-note-btn {
      background: #13100b;
      color: var(--text);
      border: 1px solid var(--border);
      padding: 6px 12px;
      border-radius: 2px;
      cursor: pointer;
      font-size: 13px;
    }
    .library-write-note-btn:hover { background: var(--panel-hover); border-color: var(--accent); color: var(--accent); }
    .library-edit-btn {
      background: transparent;
      border: 1px solid var(--border);
      color: var(--text);
      padding: 4px 10px;
      border-radius: 2px;
      cursor: pointer;
      font-size: 12px;
      margin-right: 4px;
    }
    .library-edit-btn:hover { background: var(--panel-hover); border-color: var(--accent); color: var(--accent); }
    #watches {
      position: fixed; inset: 0;
      background: rgba(10,10,10,0.6);
      display: none; align-items: center; justify-content: center;
      z-index: 112; backdrop-filter: blur(4px);
    }
    #watches.open { display: flex; }

    /* Memory pane rows — compact card-ish list with a delete button
       per row. Sensitive-encrypted rows show a lock icon and the
       content is redacted to `(encrypted)` by the server. */
    .mem-row {
      display: flex; gap: 10px;
      padding: 10px 12px;
      border-bottom: 1px solid var(--border);
      align-items: flex-start;
    }
    .mem-row:last-child { border-bottom: 0; }
    .mem-row .mem-body { flex: 1; min-width: 0; }
    .mem-row .mem-tag {
      display: inline-block;
      font-size: 10px;
      text-transform: uppercase;
      letter-spacing: 0.04em;
      color: var(--accent);
      background: rgba(255, 176, 0, 0.15);
      padding: 1px 6px;
      border-radius: 2px;
      margin-right: 6px;
      font-weight: 600;
    }
    .mem-row .mem-tag.convo { background: rgba(156, 156, 156, 0.15); color: var(--text-muted); }
    .mem-row .mem-tag.enc { background: rgba(245, 158, 11, 0.18); color: #fbbf24; }
    .mem-row .mem-content {
      font-size: 13px;
      word-break: break-word;
      line-height: 1.4;
      margin-top: 3px;
    }
    .mem-row .mem-meta {
      color: var(--text-muted);
      font-size: 11px;
      margin-top: 4px;
    }
    .mem-row .mem-del {
      flex-shrink: 0;
      background: transparent;
      color: var(--text-muted);
      border: 1px solid var(--border);
      border-radius: 2px;
      padding: 4px 10px;
      font: inherit;
      font-size: 12px;
      cursor: pointer;
    }
    .mem-row .mem-del:hover { color: var(--danger); border-color: rgba(239, 68, 68, 0.3); background: rgba(239,68,68,0.05); }
    .watch-row {
      display: flex; gap: 12px; padding: 12px 16px;
      border-bottom: 1px solid var(--border);
      align-items: flex-start;
    }
    .watch-row:last-child { border-bottom: 0; }
    .watch-cond {
      flex: 1; font-size: 14px; line-height: 1.4;
      word-break: break-word;
    }
    .watch-meta {
      font-size: 11px; color: var(--text-muted);
      margin-top: 4px;
    }
    .watch-pill {
      display: inline-block;
      padding: 1px 7px;
      border-radius: 2px;
      font-size: 10px;
      text-transform: uppercase;
      letter-spacing: 0.04em;
      font-weight: 600;
      margin-right: 6px;
    }
    .watch-pill.active { background: rgba(255, 176, 0, 0.15); color: var(--accent); }
    .watch-pill.inactive { background: rgba(156, 156, 156, 0.15); color: var(--text-muted); }
    .watch-actions { display: flex; gap: 4px; flex-shrink: 0; }
    .watch-actions button {
      background: transparent; border: 1px solid var(--border);
      color: var(--text-muted); padding: 5px 10px;
      border-radius: 2px; font: inherit; font-size: 12px;
      cursor: pointer;
    }
    .watch-actions button:hover { background: var(--panel-hover); color: var(--text); }
    .watch-actions button.danger { color: var(--danger); border-color: rgba(239,68,68,0.3); }
    .watch-actions button.danger:hover { background: rgba(239,68,68,0.1); }
    .library-panel {
      background: var(--panel);
      border: 1px solid var(--border);
      border-radius: 2px;
      width: 760px;
      max-width: 94vw;
      height: 620px;
      max-height: 88vh;
      display: flex;
      flex-direction: column;
      overflow: hidden;
      box-shadow: 0 24px 60px rgba(0,0,0,0.5);
    }
    .library-head {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 16px 20px 14px;
      border-bottom: 1px solid var(--border);
    }
    .library-head h2 {
      margin: 0;
      font-family: var(--font-display);
      font-size: 24px;
      font-weight: 400;
      letter-spacing: 0.02em;
      color: var(--accent);
      text-shadow: 0 0 8px var(--accent-glow);
    }
    .library-head .sub {
      color: var(--text-muted);
      font-size: 13px;
      margin-top: 2px;
    }
    .library-close {
      background: transparent;
      border: 0;
      color: var(--text-muted);
      cursor: pointer;
      padding: 4px 10px;
      border-radius: 2px;
      font-size: 22px;
      line-height: 1;
    }
    .library-close:hover { background: var(--panel-hover); color: var(--text); }

    .library-upload {
      padding: 16px 20px;
      border-bottom: 1px solid var(--border);
      display: flex;
      flex-direction: column;
      gap: 10px;
    }
    .library-dropzone {
      border: 1.5px dashed #4a4a4a;
      border-radius: 2px;
      padding: 20px;
      text-align: center;
      color: var(--text-muted);
      font-size: 14px;
      transition: background 0.12s, border-color 0.12s;
      cursor: pointer;
    }
    .library-dropzone:hover,
    .library-dropzone.dragover {
      background: #13100b;
      border-color: var(--accent);
      color: var(--text);
    }
    .library-upload-row {
      display: flex;
      gap: 10px;
      align-items: center;
    }
    .library-upload-row label {
      font-size: 12px;
      color: var(--text-muted);
      text-transform: uppercase;
      letter-spacing: 0.05em;
    }
    .library-upload-row select {
      background: #13100b;
      color: var(--text);
      border: 1px solid var(--border);
      border-radius: 2px;
      padding: 8px 10px;
      font: inherit;
      font-size: 14px;
    }
    .library-upload-row .status {
      flex: 1;
      color: var(--text-muted);
      font-size: 13px;
      min-height: 16px;
    }
    .library-upload-row .status.ok { color: var(--accent); }
    .library-upload-row .status.err { color: var(--danger); }

    .library-list {
      flex: 1;
      overflow-y: auto;
      padding: 12px 20px 20px;
    }
    .library-filter {
      margin-bottom: 10px;
    }
    .library-filter input {
      width: 100%;
      background: #13100b;
      color: var(--text);
      border: 1px solid var(--border);
      border-radius: 2px;
      padding: 9px 12px;
      font: inherit;
      font-size: 14px;
    }
    .library-table {
      width: 100%;
      border-collapse: collapse;
      background: #13100b;
      border: 1px solid var(--border);
      border-radius: 2px;
      overflow: hidden;
    }
    .library-table th {
      text-align: left;
      font-size: 11px;
      text-transform: uppercase;
      letter-spacing: 0.05em;
      color: var(--text-muted);
      font-weight: 600;
      padding: 8px 12px;
      border-bottom: 1px solid var(--border);
      background: var(--panel);
    }
    .library-table td {
      padding: 10px 12px;
      border-bottom: 1px solid var(--border);
      vertical-align: middle;
      font-size: 13px;
    }
    .library-table tr:last-child td { border-bottom: 0; }
    .library-table .doc-name {
      font-weight: 500;
      max-width: 300px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .library-scope-badge {
      display: inline-block;
      padding: 2px 8px;
      border-radius: 2px;
      font-size: 11px;
      font-weight: 600;
      letter-spacing: 0.04em;
      text-transform: uppercase;
    }
    .scope-global { background: rgba(255,176,0,0.15); color: var(--accent); }
    .scope-conv   { background: rgba(156,156,156,0.15); color: var(--text-muted); }
    .library-del-btn {
      background: transparent;
      border: 1px solid rgba(239,68,68,0.3);
      color: var(--danger);
      padding: 4px 10px;
      border-radius: 2px;
      font: inherit;
      font-size: 12px;
      cursor: pointer;
    }
    .library-del-btn:hover { background: rgba(239,68,68,0.1); }
    .library-empty {
      padding: 40px 20px;
      text-align: center;
      color: var(--text-muted);
      font-style: italic;
    }

    /* Keyboard shortcuts overlay. Press ? outside an input. */
    #shortcuts {
      position: fixed;
      inset: 0;
      background: rgba(10,10,10,0.6);
      display: none;
      align-items: center;
      justify-content: center;
      z-index: 115;
      backdrop-filter: blur(4px);
    }
    #shortcuts.open { display: flex; }
    .shortcuts-panel {
      background: var(--panel);
      border: 1px solid var(--border);
      border-radius: 2px;
      padding: 22px 26px;
      min-width: 420px;
      max-width: 92vw;
      box-shadow: 0 24px 60px rgba(0,0,0,0.5);
    }
    .shortcuts-panel h2 {
      margin: 0 0 6px;
      font-family: var(--font-display);
      font-size: 24px;
      font-weight: 400;
      letter-spacing: 0.02em;
      color: var(--accent);
      text-shadow: 0 0 8px var(--accent-glow);
    }
    .shortcuts-panel p.hint {
      color: var(--text-muted);
      font-size: 13px;
      margin: 0 0 18px;
    }
    .shortcuts-grid {
      display: grid;
      grid-template-columns: 1fr auto;
      gap: 10px 18px;
      align-items: center;
    }
    .shortcuts-grid dt {
      font-size: 14px;
      color: var(--text);
    }
    .shortcuts-grid dd {
      margin: 0;
      display: inline-flex;
      gap: 4px;
      align-items: center;
    }
    .kbd {
      background: #13100b;
      border: 1px solid var(--border);
      border-bottom-width: 2px;
      border-radius: 2px;
      padding: 2px 8px;
      font-family: "Geist Mono", "SF Mono", monospace;
      font-size: 12px;
      color: var(--text);
      white-space: nowrap;
    }

    /* Sidebar footer — compact user chip that opens settings. Not a
       full user menu (that lives in the header); this is the
       conventional lower-left anchor point + fast path to settings. */
    .sidebar-footer {
      border-top: 1px solid var(--border);
      padding: 10px 8px;
    }
    .sidebar-user {
      display: flex;
      align-items: center;
      gap: 10px;
      width: 100%;
      background: transparent;
      border: 0;
      padding: 8px 10px;
      border-radius: 2px;
      color: var(--text);
      font: inherit;
      cursor: pointer;
      text-align: left;
    }
    .sidebar-user:hover { background: var(--panel); }
    .sidebar-user .avatar {
      width: 28px;
      height: 28px;
      border-radius: 50%;
      background: linear-gradient(135deg, #c97d4a 0%, #8b5a3a 100%);
      color: #fff;
      font-size: 12px;
      font-weight: 600;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      text-transform: uppercase;
      flex-shrink: 0;
    }
    .sidebar-user .lines {
      flex: 1;
      min-width: 0;
      display: flex;
      flex-direction: column;
      gap: 1px;
    }
    .sidebar-user .name {
      font-size: 13px;
      font-weight: 500;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .sidebar-user .sub {
      font-size: 11px;
      color: var(--text-muted);
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .sidebar-user .cog {
      color: var(--text-muted);
      display: inline-flex;
      flex-shrink: 0;
    }
    .sidebar-user:hover .cog { color: var(--text); }

    /* ---------- Mobile ---------- */
    @media (max-width: 800px) {
      .sidebar {
        position: fixed;
        top: 0; left: 0; bottom: 0;
        z-index: 50;
        margin-left: calc(-1 * var(--sidebar-w));
        box-shadow: 2px 0 12px rgba(0,0,0,0.5);
      }
      .sidebar.open { margin-left: 0; }
      .topbar .burger { display: inline-flex; }
      .user-pill .username { display: none; }
      .input-wrap { left: 0; }
      /* Welcome chips: 2-col compact on mobile, hide the overflow past
         the top 4 so a fresh tab doesn't require a scroll. */
      .welcome { padding: 40px 16px 20px; }
      .welcome h2 { font-size: 22px; }
      .welcome-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 8px;
        max-width: none;
      }
      .welcome-chip { padding: 10px 12px; font-size: 12px; }
      .welcome-chip .label { font-size: 10px; margin-bottom: 2px; }
      .welcome-chip:nth-child(n+5) { display: none; }
    }
