html[data-app-rail="knowledge"] {
  --kb-companion-width: clamp(340px, 24vw, 392px);
  --kb-companion-accent: var(--sv-color-brand);
}

html[data-app-rail="knowledge"][data-knowledge-mode="vault"] .kb-layout {
  grid-template-columns: 218px minmax(0, 1fr) var(--kb-companion-width);
}

html[data-app-rail="knowledge"][data-knowledge-mode="vault"][data-knowledge-context="collapsed"] .kb-layout {
  grid-template-columns: 218px minmax(0, 1fr);
}

.kb-writing-companion {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-width: 0;
  overflow: hidden;
  background: var(--sv-color-surface);
}

.kb-writing-companion > .kb-companion-head {
  position: static;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto 28px;
  align-items: center;
  gap: 10px;
  min-height: 58px;
  padding: 0 10px 0 15px;
  border-bottom: 1px solid var(--sv-color-border);
  background: var(--sv-color-surface);
}

.kb-companion-identity {
  display: grid;
  gap: 3px;
  min-width: 0;
}

.kb-companion-identity small {
  color: var(--sv-color-text-muted);
  font: 650 8px/1 var(--sv-font-sans);
  letter-spacing: .13em;
}

.kb-companion-identity strong {
  color: var(--sv-color-text);
  font: 720 15px/1.2 var(--sv-font-sans);
  letter-spacing: -.02em;
}

.kb-companion-head nav {
  display: flex;
  gap: 2px;
  padding: 2px;
  border-radius: 6px;
  background: var(--sv-color-selection-bg);
}

.kb-companion-head nav button {
  width: auto;
  height: 26px;
  padding: 0 8px;
  border: 0;
  border-radius: 4px;
  color: var(--sv-color-text-muted);
  background: transparent;
  font: 650 9px/1 var(--sv-font-sans);
  cursor: pointer;
  transition: color 160ms ease, background 160ms ease;
}

.kb-companion-head nav button:hover,
.kb-companion-head nav button:focus-visible {
  color: var(--sv-color-text);
  outline: 0;
}

.kb-companion-head nav button.is-active {
  color: var(--sv-color-text);
  background: var(--sv-color-surface);
  box-shadow: 0 1px 3px rgba(62, 51, 41, .08);
}

.kb-writing-companion > .kb-ai-drawer {
  position: static;
  z-index: auto;
  display: grid;
  grid-template-rows: auto auto auto minmax(0, 1fr) auto;
  width: 100%;
  height: auto;
  min-height: 0;
  border: 0;
  color: var(--sv-color-text);
  background: var(--sv-color-surface);
  box-shadow: none;
  transform: none;
  transition: none;
}

.kb-writing-companion > .kb-ai-drawer > .kb-ai-capabilities { grid-row: 1; }
.kb-writing-companion > .kb-ai-drawer > .kb-ai-context { grid-row: 2; }
.kb-writing-companion > .kb-ai-drawer > .kb-ai-shortcuts { grid-row: 3; }
.kb-writing-companion > .kb-ai-drawer > .kb-ai-messages { grid-row: 4; }
.kb-writing-companion > .kb-ai-drawer > .kb-ai-compose { grid-row: 5; }

.kb-ai-capabilities {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 6px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--sv-color-border);
}

.kb-ai-capabilities label {
  display: grid;
  gap: 5px;
  min-width: 0;
}

.kb-ai-capability {
  display: grid;
  min-width: 0;
}

.kb-ai-expert-capability,
.kb-ai-skill-capability {
  display: block;
}

.kb-expert-picker-trigger {
  display: grid;
  gap: 5px;
  width: 100%;
  min-height: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  color: var(--sv-color-text-muted);
  background: transparent;
  text-align: left;
  cursor: pointer;
}

.kb-expert-picker-trigger > span {
  color: var(--sv-color-text-muted);
  font: 650 8px/1 var(--sv-font-sans);
  letter-spacing: .06em;
}

.kb-expert-picker-trigger > strong {
  display: block;
  overflow: hidden;
  height: 30px;
  padding: 0 26px 0 8px;
  border: 1px solid var(--sv-color-border);
  border-radius: 6px;
  color: var(--sv-color-text-muted);
  background:
    linear-gradient(45deg, transparent 50%, var(--sv-color-text-muted) 50%) calc(100% - 10px) calc(50% - 2px) / 5px 5px no-repeat,
    linear-gradient(135deg, var(--sv-color-text-muted) 50%, transparent 50%) calc(100% - 6px) calc(50% - 2px) / 5px 5px no-repeat,
    var(--sv-color-surface);
  font: 550 10px/30px var(--sv-font-sans);
  text-overflow: ellipsis;
  white-space: nowrap;
  transition: border-color .16s ease, color .16s ease;
}

.kb-expert-picker-trigger:hover > strong,
.kb-expert-picker-trigger:focus-visible > strong {
  color: var(--sv-color-text);
  border-color: var(--sv-color-border);
  outline: 0;
}

.kb-expert-picker-trigger:focus-visible {
  outline: 0;
}

.kb-ai-capabilities label > span {
  color: var(--sv-color-text-muted);
  font: 650 8px/1 var(--sv-font-sans);
  letter-spacing: .06em;
}

.kb-ai-capabilities select {
  width: 100%;
  min-width: 0;
  height: 30px;
  padding: 0 26px 0 8px;
  border: 1px solid var(--sv-color-border);
  border-radius: 6px;
  color: var(--sv-color-text-muted);
  background: var(--sv-color-surface);
  font: 550 10px/1 var(--sv-font-sans);
}

.kb-expert-picker-dialog {
  width: min(860px, calc(100vw - 40px));
  max-height: min(700px, calc(100dvh - 44px));
  padding: 0;
  border: 1px solid var(--sv-color-border);
  border-radius: 14px;
  color: var(--sv-color-text);
  background: var(--sv-color-surface);
  box-shadow: 0 28px 80px rgba(28, 25, 22, .24);
  overflow: hidden;
  font-family: var(--sv-font-sans);
}

.kb-expert-picker-dialog::backdrop {
  background: var(--sv-color-surface);
  backdrop-filter: blur(5px);
}

.kb-expert-picker-dialog > header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
  padding: 18px 20px 14px;
  border-bottom: 1px solid var(--sv-color-border);
}

.kb-expert-picker-dialog > header > div {
  display: grid;
  gap: 4px;
}

.kb-expert-picker-dialog > header small {
  color: var(--sv-color-text-muted);
  font: 650 8px/1 inherit;
  letter-spacing: .12em;
}

.kb-expert-picker-dialog > header strong {
  color: var(--sv-color-text);
  font: 700 18px/1.25 inherit;
  letter-spacing: -.01em;
}

.kb-expert-picker-dialog > header p {
  max-width: 520px;
  margin: 0;
  color: var(--sv-color-text-faint);
  font: 400 11px/1.45 inherit;
}

.kb-expert-picker-dialog > header > button {
  flex: 0 0 auto;
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 50%;
  color: var(--sv-color-text-faint);
  background: var(--sv-color-surface);
  font-size: 20px;
  cursor: pointer;
}

.kb-expert-picker-body {
  display: grid;
  grid-template-columns: minmax(280px, 1fr) minmax(260px, .9fr);
  min-height: 390px;
  max-height: min(500px, calc(100dvh - 180px));
}

.kb-expert-picker-library {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 10px;
  padding: 14px;
  border-right: 1px solid var(--sv-color-border);
  min-width: 0;
  overflow: hidden;
}

.kb-expert-picker-library > label {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 34px;
  padding: 0 10px;
  border: 1px solid var(--sv-color-border);
  border-radius: 8px;
  background: var(--sv-color-surface);
  box-shadow: none;
  outline: 0;
  transition: border-color .14s ease, box-shadow .14s ease;
}

.kb-expert-picker-library > label:hover {
  border-color: var(--sv-color-border);
}

.kb-expert-picker-library > label:focus-within {
  border-color: var(--sv-color-border);
  box-shadow: 0 0 0 2px rgba(91, 82, 72, .08);
  outline: 0;
}

.kb-expert-picker-library > label span {
  color: var(--sv-color-text-muted);
}

.kb-expert-picker-library > label input {
  width: 100%;
  border: 0;
  outline: 0;
  color: var(--sv-color-text);
  background: transparent;
  font: 500 12px/1 inherit;
  box-shadow: none;
  accent-color: var(--sv-color-text-faint);
}

.kb-expert-picker-library > label input:focus,
.kb-expert-picker-library > label input:focus-visible {
  border: 0 !important;
  outline: 0 !important;
  box-shadow: none !important;
}

.kb-expert-picker-list {
  display: grid;
  align-content: start;
  gap: 6px;
  overflow: auto;
  padding-right: 2px;
}

.kb-expert-card {
  display: grid;
  grid-template-columns: 40px minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  width: 100%;
  padding: 9px 10px;
  border: 1px solid transparent;
  border-radius: 9px;
  color: var(--sv-color-text-muted);
  background: var(--sv-color-surface);
  text-align: left;
  cursor: pointer;
  transition: border-color .16s ease, background .16s ease;
}

.kb-expert-card:hover {
  border-color: var(--sv-color-border);
  background: var(--sv-color-surface);
}

.kb-expert-card.is-selected {
  border-color: var(--sv-color-border);
  background: var(--sv-color-surface);
  box-shadow: none;
}

.kb-expert-card-avatar,
.kb-expert-detail-avatar {
  display: grid;
  place-items: center;
  overflow: hidden;
  border-radius: 9px;
  color: var(--sv-color-text);
  background: var(--sv-color-surface-hover);
  font: 700 14px/1 inherit;
}

.kb-expert-card-avatar {
  width: 40px;
  height: 40px;
}

.kb-expert-card-avatar img,
.kb-expert-detail-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.kb-expert-card-main {
  display: grid;
  gap: 3px;
  min-width: 0;
}

.kb-expert-card-main strong {
  overflow: hidden;
  color: var(--sv-color-text);
  font: 600 13px/1.3 var(--sv-font-sans);
  letter-spacing: 0;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.kb-expert-card-main span {
  overflow: hidden;
  color: var(--sv-color-text-faint);
  font: 400 10px/1.35 inherit;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.kb-expert-card > i {
  color: var(--sv-color-text-muted);
  font: 600 11px/1 inherit;
  font-style: normal;
}

.kb-expert-picker-empty {
  display: grid;
  place-items: center;
  min-height: 160px;
  padding: 24px;
  border: 1px dashed var(--sv-color-border);
  border-radius: 10px;
  color: var(--sv-color-text-muted);
  font: 400 12px/1.6 inherit;
  text-align: center;
}

.kb-expert-picker-detail {
  min-width: 0;
  padding: 18px 18px 20px;
  overflow: auto;
  background: var(--sv-color-surface);
}

.kb-expert-detail-empty {
  display: grid;
  gap: 8px;
  align-content: start;
  min-height: 100%;
  color: var(--sv-color-text-faint);
}

.kb-expert-detail-empty span {
  color: var(--sv-color-text-muted);
  font: 650 8px/1 inherit;
  letter-spacing: .1em;
}

.kb-expert-detail-empty strong {
  color: var(--sv-color-text);
  font: 700 14px/1.3 inherit;
}

.kb-expert-detail-empty p {
  margin: 0;
  color: var(--sv-color-text-faint);
  font: 400 11px/1.55 inherit;
}

.kb-expert-detail-head {
  display: grid;
  grid-template-columns: 48px minmax(0, 1fr);
  gap: 12px;
  align-items: center;
}

.kb-expert-detail-avatar {
  width: 48px;
  height: 48px;
  border-radius: 11px;
  font-size: 16px;
}

.kb-expert-detail-head div {
  display: grid;
  gap: 3px;
  min-width: 0;
}

.kb-expert-detail-head small {
  color: var(--sv-color-text-muted);
  font: 500 10px/1.3 inherit;
}

.kb-expert-detail-head strong {
  overflow: hidden;
  color: var(--sv-color-text);
  font: 700 15px/1.25 inherit;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.kb-expert-detail-head span {
  width: fit-content;
  padding: 2px 7px;
  border-radius: 999px;
  color: var(--sv-color-text-faint);
  background: var(--sv-color-surface);
  font: 500 9px/1.4 inherit;
}

.kb-expert-detail-description {
  margin: 12px 0 0;
  color: var(--sv-color-text-faint);
  font: 400 11px/1.55 inherit;
}

.kb-expert-detail-section {
  display: grid;
  gap: 8px;
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--sv-color-border);
}

.kb-expert-detail-section > header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}

.kb-expert-detail-section > header strong {
  color: var(--sv-color-text);
  font: 650 11px/1.2 inherit;
}

.kb-expert-detail-section > header span {
  color: var(--sv-color-text-muted);
  font: 500 10px/1 inherit;
}

.kb-expert-detail-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.kb-expert-detail-tags span,
.kb-expert-detail-tags button {
  max-width: 100%;
  padding: 4px 8px;
  border: 0;
  border-radius: 6px;
  color: var(--sv-color-text-faint);
  background: var(--sv-color-surface);
  font: 500 10px/1.3 inherit;
  word-break: break-word;
  text-align: left;
  cursor: default;
}

.kb-expert-detail-tags button {
  cursor: pointer;
}

.kb-expert-detail-tags button:hover {
  color: var(--sv-color-text);
  background: var(--sv-color-surface);
}

.kb-expert-detail-section > p {
  margin: 0;
  color: var(--sv-color-text-muted);
  font: 400 10px/1.5 inherit;
}

.kb-expert-detail-tasks {
  display: grid;
  gap: 7px;
  margin: 0;
  padding: 0;
  list-style: none;
  counter-reset: expert-task;
}

.kb-expert-detail-tasks li {
  display: grid;
  grid-template-columns: 16px minmax(0, 1fr);
  gap: 8px;
  color: var(--sv-color-text-faint);
  font: 400 11px/1.45 inherit;
  counter-increment: expert-task;
}

.kb-expert-detail-tasks li::before {
  content: counter(expert-task);
  color: var(--sv-color-text-muted);
  font: 600 10px/1.45 inherit;
}

.kb-expert-picker-dialog > footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 16px;
  border-top: 1px solid var(--sv-color-border);
  background: var(--sv-color-surface);
}

.kb-expert-picker-dialog > footer > span {
  color: var(--sv-color-text-faint);
  font: 400 11px/1.4 inherit;
}

.kb-expert-picker-dialog > footer > div {
  display: flex;
  gap: 8px;
}

.kb-expert-picker-dialog > footer button {
  min-height: 34px;
  padding: 0 14px;
  border: 1px solid var(--sv-color-border);
  border-radius: 7px;
  color: var(--sv-color-text-faint);
  background: var(--sv-color-surface);
  font: 650 11px/1 inherit;
  cursor: pointer;
}

.kb-expert-picker-dialog > footer button.is-primary {
  color: var(--sv-color-text);
  border-color: var(--sv-color-border);
  background: var(--sv-color-surface-hover);
}

.kb-expert-picker-dialog > footer button:disabled {
  opacity: .5;
  cursor: wait;
}

@media (max-width: 720px) {
  .kb-expert-picker-body {
    grid-template-columns: 1fr;
    max-height: calc(100dvh - 180px);
    overflow: auto;
  }

  .kb-expert-picker-library {
    min-height: 280px;
    border-right: 0;
    border-bottom: 1px solid var(--sv-color-border);
  }

  .kb-expert-picker-detail {
    overflow: visible;
  }

  .kb-expert-picker-dialog > footer {
    align-items: flex-start;
  }
}

.kb-writing-companion .kb-ai-context {
  padding: 9px 12px;
  border-bottom: 1px solid var(--sv-color-border);
  background: var(--sv-color-surface);
}

.kb-writing-companion .kb-ai-context > span {
  color: var(--sv-color-text-faint);
  font: 650 9px/1.2 var(--sv-font-sans);
}

.kb-writing-companion .kb-ai-context > p {
  display: -webkit-box;
  overflow: hidden;
  margin: 5px 0 0;
  color: var(--sv-color-text-muted);
  font: 400 9.5px/1.5 var(--sv-font-sans);
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
}

.kb-writing-companion .kb-ai-welcome strong {
  font-size: 12px;
}

.kb-writing-companion .kb-ai-welcome p {
  font-size: 10px;
  line-height: 1.55;
}

.kb-writing-companion .kb-ai-shortcuts {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.kb-writing-companion[data-companion-mode="planning"] .kb-ai-shortcuts {
  display: none;
}

.kb-writing-companion .kb-ai-messages {
  min-height: 0;
}

.kb-writing-companion .kb-ai-compose {
  align-self: end;
  grid-template-rows: auto 84px auto;
  padding-bottom: 12px;
}

.kb-writing-companion .kb-ai-compose textarea {
  height: 84px;
  min-height: 84px;
  max-height: 160px;
}

html[data-app-rail="knowledge"][data-knowledge-mode="vault"] .kb-editor-pane.is-empty {
  grid-template-rows: minmax(0, 1fr);
  place-items: center;
}

html[data-app-rail="knowledge"][data-knowledge-mode="vault"] .kb-editor-pane.is-empty .kb-editor-empty {
  width: min(100%, 560px);
  max-width: 560px;
  padding: 0 32px;
  transform: translateY(-6vh);
}

.kb-context-structure {
  min-height: 0;
  overflow: auto;
}

.kb-context-structure[hidden],
.kb-writing-companion > .kb-ai-drawer[hidden] {
  display: none;
}

.kb-context-reopen {
  width: 30px;
  min-height: 104px;
  height: auto;
  padding: 9px 4px;
  writing-mode: vertical-rl;
  text-orientation: mixed;
  letter-spacing: .08em;
}

@media (max-width: 1280px) {
  html[data-app-rail="knowledge"] { --kb-companion-width: 330px; }
  html[data-app-rail="knowledge"][data-knowledge-mode="vault"] .kb-layout {
    grid-template-columns: 204px minmax(0, 1fr) var(--kb-companion-width);
  }
}

@media (max-width: 980px) {
  html[data-app-rail="knowledge"][data-knowledge-mode="vault"] .kb-layout,
  html[data-app-rail="knowledge"][data-knowledge-mode="vault"][data-knowledge-context="collapsed"] .kb-layout {
    grid-template-columns: 200px minmax(0, 1fr);
  }

  html[data-app-rail="knowledge"][data-knowledge-mode="vault"] .kb-writing-companion {
    display: none;
  }
}

/* Same theme for the companion header, messages, inspector and composer. */
html[data-app-rail="knowledge"] .kb-writing-companion,
html[data-app-rail="knowledge"] .kb-writing-companion > .kb-ai-drawer {
  color: var(--sv-color-text);
  background: var(--sv-color-reading-companion);
}
html[data-app-rail="knowledge"] .kb-writing-companion > .kb-companion-head,
html[data-app-rail="knowledge"] .kb-writing-companion .kb-ai-context,
html[data-app-rail="knowledge"] .kb-writing-companion .kb-ai-compose {
  color: var(--sv-color-text);
  background: var(--sv-color-surface-strong);
}
html[data-app-rail="knowledge"] .kb-writing-companion .kb-ai-welcome strong { color: var(--sv-color-text); }

/* Neutral light theme: semantic surfaces, paired foregrounds. */

html[data-theme="light"][data-app-rail="knowledge"] .kb-writing-companion .kb-ai-messages { background:var(--sv-color-reading-companion); }
html[data-theme="light"][data-app-rail="knowledge"] .kb-writing-companion :is(.kb-ai-welcome,.kb-ai-compose textarea) { background:var(--sv-color-surface); border:1px solid var(--sv-color-border); border-radius:10px; }
html[data-theme="light"][data-app-rail="knowledge"] #kbAiRun { background:var(--sv-color-brand); color:var(--sv-color-on-brand); }


/* Light companion keeps large surfaces quiet and neutral. */
html[data-theme="light"][data-app-rail="knowledge"] .kb-writing-companion > .kb-companion-head,
html[data-theme="light"][data-app-rail="knowledge"] .kb-writing-companion :is(.kb-ai-context,.kb-ai-compose) { background:var(--sv-color-surface-subtle); }
