:root {
  color-scheme: light dark;
  --font-main: "Space Grotesk", ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  --bg: #f4f5f2;
  --surface: #ffffff;
  --surface-soft: #f9faf7;
  --surface-strong: #edf3ef;
  --ink: #161b18;
  --muted: #5f6a62;
  --subtle: #879189;
  --line: #d9dfd8;
  --line-strong: #c6cec6;
  --accent: #0f766e;
  --accent-strong: #0a5e58;
  --accent-soft: #e5f3ef;
  --focus: #1d9a8e;
  --shadow: 0 18px 48px rgba(24, 35, 29, 0.14);
  --shadow-soft: 0 1px 2px rgba(24, 35, 29, 0.06);
  --radius: 8px;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --text-xs: 12px;
  --text-sm: 14px;
  --text-base: 16px;
  --text-lg: 18px;
  --text-xl: 20px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111512;
    --surface: #191f1b;
    --surface-soft: #151a17;
    --surface-strong: #1f2a25;
    --ink: #e7ece8;
    --muted: #a8b2aa;
    --subtle: #7f8b83;
    --line: #303a34;
    --line-strong: #425048;
    --accent: #37b5a8;
    --accent-strong: #67d0c7;
    --accent-soft: #16362f;
    --shadow: none;
    --shadow-soft: none;
  }
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

[hidden] {
  display: none !important;
}

html {
  min-width: 320px;
  overscroll-behavior: none;
}

body {
  margin: 0;
  min-height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  font-family: var(--system-font, var(--font-main));
  font-size: var(--text-base);
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button,
input,
select,
textarea {
  font: inherit;
  font-size: 15px;
}

button {
  min-height: 38px;
  border: 0;
  border-radius: var(--radius);
  padding: 0 var(--space-3);
  background: var(--accent);
  color: #ffffff;
  font-weight: 650;
  box-shadow: var(--shadow-soft);
  cursor: pointer;
  transition: background 150ms ease, box-shadow 150ms ease, transform 120ms ease;
}

button:hover {
  background: var(--accent-strong);
}

button:active {
  transform: translateY(1px) scale(0.99);
  box-shadow: none;
}

button:disabled {
  opacity: 0.55;
  cursor: wait;
  transform: none;
}

button.secondary,
.book-actions button,
.book-modal header button,
.icon-button,
.user-actions button,
.settings-list button.danger {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
}

button.secondary:hover,
.book-actions button:hover,
.book-modal header button:hover,
.icon-button:hover,
.user-actions button:hover,
.settings-list button.danger:hover {
  border-color: var(--line-strong);
  background: var(--surface-strong);
}

input,
select,
textarea {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 12px;
  background: var(--surface);
  color: var(--ink);
  outline: none;
  transition: border-color 150ms ease, box-shadow 150ms ease, background 150ms ease;
}

input:focus,
select:focus,
textarea:focus,
button:focus-visible,
summary:focus-visible,
a:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--focus);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 18%, transparent);
}

input::placeholder,
textarea::placeholder {
  color: color-mix(in srgb, var(--subtle) 62%, var(--surface));
  opacity: 1;
}

a {
  color: var(--accent-strong);
  font-weight: 750;
  text-underline-offset: 3px;
}

h1,
h2 {
  margin: 0;
  line-height: 1.2;
  letter-spacing: 0;
}

h1 {
  font-size: var(--text-xl);
}

h2 {
  font-size: var(--text-lg);
}

.login-screen {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: var(--space-5);
  background: var(--bg);
}

.login-card {
  display: grid;
  gap: var(--space-4);
  width: min(392px, 100%);
  padding: var(--space-6);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.login-card p,
#settings-result,
#user-result,
#workspace-result,
#mcp-result,
#upload-result,
.block-result,
.settings-head p {
  margin: 0;
  color: var(--muted);
  font-size: var(--text-xs);
  line-height: 1.45;
}

.app {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  height: 100dvh;
  min-height: 620px;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(260px, max-content) minmax(0, 1fr) minmax(170px, max-content);
  align-items: end;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-width: 0;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  flex: 0 0 auto;
  border-radius: var(--radius);
  background: var(--accent);
  color: #ffffff;
  font-family: var(--font-mono);
  font-size: 12px;
  font-weight: 850;
}

.brand.compact .brand-mark {
  width: 40px;
  height: 40px;
  font-size: 12px;
}

.brand.compact h1 {
  min-width: 0;
  overflow: visible;
  text-overflow: clip;
  white-space: normal;
  font-size: var(--text-lg);
  line-height: 1.12;
}

.topbar-work {
  display: grid;
  grid-template-columns: minmax(96px, 124px) minmax(170px, 250px) max-content max-content;
  align-items: end;
  gap: var(--space-2);
  min-width: 0;
  overflow: visible;
}

.topbar-work .language-control,
.topbar-work .workspace-control {
  display: grid;
  gap: 3px;
}

.topbar-work .language-control span,
.topbar-work .workspace-control span {
  position: static;
  width: auto;
  height: auto;
  overflow: visible;
  clip: auto;
  white-space: nowrap;
  font-size: 11px;
  line-height: 1;
  text-transform: uppercase;
  letter-spacing: 0;
}

.topbar-work .language-control select,
.topbar-work .workspace-control select,
.topbar-work button {
  height: 36px;
  min-height: 36px;
  font-size: 13px;
}

.topbar-work .language-control select,
.topbar-work .workspace-control select {
  padding: 6px 9px;
  font-size: 14px;
}

.topbar-actions button {
  min-height: 34px;
  height: 34px;
  padding: 0 10px;
  font-size: 13px;
}

.token-mini {
  align-self: end;
  justify-self: end;
  min-width: max-content;
  color: var(--muted);
  font-family: var(--font-mono);
  font-size: 10px;
  line-height: 1.18;
  text-align: right;
}

.token-mini span {
  display: block;
  overflow: visible;
  text-overflow: clip;
  white-space: nowrap;
}

#open-book-modal,
#open-references-modal {
  align-self: end;
  min-width: 0;
  white-space: nowrap;
  overflow: visible;
  text-overflow: clip;
}

.topbar-actions {
  display: flex;
  align-self: end;
  align-items: flex-end;
  justify-content: flex-end;
  gap: var(--space-3);
  min-width: max-content;
}

.account-actions {
  display: grid;
  align-items: end;
  justify-items: end;
  gap: 4px;
  min-width: max-content;
}

.logged-user-label {
  justify-self: end;
  color: var(--muted);
  font-size: 11px;
  line-height: 1;
  font-weight: 800;
  white-space: nowrap;
}

#status {
  align-self: center;
  width: fit-content;
  max-width: 100%;
  padding: 7px 11px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface-soft);
  color: var(--muted);
  font-size: var(--text-xs);
  font-weight: 800;
  white-space: nowrap;
}

.field {
  display: grid;
  gap: 6px;
  min-width: 0;
  color: var(--muted);
  font-size: var(--text-xs);
  font-weight: 750;
}

.field span,
legend {
  color: var(--muted);
  font-size: var(--text-xs);
  font-weight: 800;
}

.field span {
  display: flex;
  align-items: center;
  gap: 6px;
}

.help {
  display: inline-grid;
  place-items: center;
  width: 18px;
  min-width: 18px;
  height: 18px;
  min-height: 18px;
  padding: 0;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  background: var(--surface-soft);
  color: var(--muted);
  font-size: 11px;
  line-height: 1;
  box-shadow: none;
  cursor: help;
}

.help:hover {
  background: var(--surface-strong);
  color: var(--ink);
}

.inline-control {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: var(--space-2);
}

.collection-field .inline-control {
  align-items: end;
}

.inline-control button {
  white-space: nowrap;
}

.collection-create-row {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: var(--space-2);
  padding: var(--space-3);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-soft);
}

.hidden-upload-form {
  display: none;
}

#upload-result {
  margin: 0;
  min-height: 20px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-soft);
  overflow-wrap: anywhere;
}

.upload-progress {
  display: grid;
  gap: var(--space-2);
}

.progress-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  color: var(--muted);
  font-size: var(--text-sm);
  font-weight: 750;
}

.upload-progress progress {
  width: 100%;
  height: 12px;
  border: 0;
  border-radius: 999px;
  overflow: hidden;
  background: var(--surface-soft);
}

.upload-progress progress::-webkit-progress-bar {
  background: var(--surface-soft);
}

.upload-progress progress::-webkit-progress-value {
  background: var(--accent);
}

.upload-progress progress::-moz-progress-bar {
  background: var(--accent);
}

.upload-stage-list {
  display: grid;
  gap: 4px;
  margin: 6px 0 0;
  padding: 8px 10px;
  list-style: none;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-soft);
  font-size: 0.85em;
  line-height: 1.35;
}

.upload-stage-list li {
  display: grid;
  grid-template-columns: 14px 1fr auto;
  align-items: baseline;
  gap: 8px;
  color: var(--muted);
}

.upload-stage-list li .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--line);
  align-self: center;
  margin-left: 3px;
}

.upload-stage-list li .name {
  font-weight: 600;
}

.upload-stage-list li .info {
  color: var(--subtle);
  font-size: 0.92em;
  text-align: right;
  overflow-wrap: anywhere;
}

.upload-stage-list li[data-state="active"] .dot {
  background: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent);
}

.upload-stage-list li[data-state="active"] {
  color: var(--ink);
}

.upload-stage-list li[data-state="done"] .dot {
  background: var(--accent);
}

.upload-stage-list li[data-state="done"] {
  color: var(--ink);
}

.upload-stage-list li[data-state="failed"] .dot {
  background: #e25555;
}

.upload-stage-list li[data-state="failed"] {
  color: #e25555;
}

.upload-progress-detail {
  margin: 0;
  font-size: 0.85em;
  color: var(--muted);
  overflow-wrap: anywhere;
}

.upload-progress-elapsed {
  margin: 0;
  font-size: 0.78em;
  color: var(--subtle);
}

.message.pending .pending-text {
  margin-left: 6px;
  color: var(--ink);
}

.message.pending .pending-phase {
  display: block;
  margin-top: 4px;
  font-size: 0.82em;
  color: var(--subtle);
  overflow-wrap: anywhere;
}

.workspace-locked {
  margin: 4px 0 0;
  font-size: 0.82em;
  color: var(--muted);
}

.workspace-model {
  margin: 4px 0 0;
  font-size: 0.78em;
  font-family: var(--font-mono);
  color: var(--subtle);
  overflow-wrap: anywhere;
}

.workspace-quality {
  margin: 2px 0 0;
  font-size: 0.78em;
  font-family: var(--font-mono);
  color: var(--muted);
}

.workspace-badge {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 6px;
  border-radius: 999px;
  font-size: 0.7em;
  font-weight: 600;
  vertical-align: middle;
  white-space: nowrap;
}

.workspace-badge-hybrid {
  background: var(--accent-soft);
  color: var(--accent-strong);
  border: 1px solid var(--accent);
}

.workspace-badge-dense {
  background: var(--surface-strong);
  color: var(--muted);
  border: 1px solid var(--line);
}

.hybrid-hint {
  margin-left: 8px;
  font-size: 0.85em;
  color: var(--subtle);
}

.block-hint {
  margin: 0 0 var(--space-3);
  color: var(--muted);
  font-size: 0.88em;
  line-height: 1.4;
}

.block-hint code {
  font-family: var(--font-mono);
  font-size: 0.92em;
  background: var(--surface-soft);
  padding: 1px 4px;
  border-radius: var(--radius);
}

.block-subhead {
  margin: var(--space-4) 0 var(--space-2);
  font-size: 0.92em;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.block-row {
  display: flex;
  gap: var(--space-2);
  align-items: center;
  margin: var(--space-2) 0;
}

.block-row input {
  flex: 1;
}

.mcp-new-key {
  display: grid;
  gap: 6px;
  padding: 10px 12px;
  margin: 8px 0;
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  background: var(--accent-soft);
}

.mcp-new-key code {
  font-family: var(--font-mono);
  font-size: 0.85em;
  overflow-wrap: anywhere;
  background: var(--surface);
  padding: 6px 8px;
  border-radius: var(--radius);
}

.mcp-keys-list {
  display: grid;
  gap: 6px;
  margin-top: 8px;
}

.mcp-key-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--space-3);
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

.mcp-key-row.mcp-key-revoked {
  opacity: 0.55;
}

.mcp-key-row code {
  font-family: var(--font-mono);
  font-size: 0.82em;
  margin-left: 6px;
  background: var(--surface-soft);
  padding: 1px 4px;
  border-radius: var(--radius);
}

.mcp-key-row p {
  margin: 4px 0 0;
  font-size: 0.78em;
  color: var(--subtle);
}

.mcp-key-row button.danger {
  background: transparent;
  color: #e25555;
  border: 1px solid #e25555;
}

.mcp-key-row button.danger:hover {
  background: #e25555;
  color: #fff;
}

#workspace-form input:disabled,
#workspace-form select:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.workspace-form-actions {
  display: flex;
  gap: var(--space-2);
  align-items: center;
  flex-wrap: wrap;
}

.confirm-modal {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0;
  background: var(--surface);
  color: var(--ink);
  max-width: min(520px, calc(100vw - 32px));
  box-shadow: var(--shadow);
}

.confirm-modal::backdrop {
  background: rgba(17, 21, 18, 0.42);
}

.confirm-modal form {
  display: grid;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-5);
}

.confirm-modal header h2 {
  margin: 0;
  font-size: var(--text-lg);
}

.confirm-warn {
  margin: 0;
  color: var(--ink);
  line-height: 1.45;
}

.confirm-effects {
  margin: 0;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-soft);
  color: var(--muted);
  font-size: 0.92em;
  line-height: 1.4;
}

.confirm-mismatch {
  margin: 0;
  color: #e25555;
  font-size: 0.85em;
}

.embedding-impact-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: 50vh;
  overflow-y: auto;
}

.embedding-impact-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-soft);
}

.embedding-impact-row strong {
  color: var(--ink);
  font-size: 0.95em;
}

.embedding-impact-diff {
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.82em;
  line-height: 1.4;
  word-break: break-word;
}

.confirm-footer {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
  border: 0;
  padding: 0;
}

.confirm-footer button.danger {
  background: #c53030;
  color: #fff;
  border: 1px solid #a02222;
}

.confirm-footer button.danger:disabled {
  background: var(--surface-strong);
  color: var(--subtle);
  border-color: var(--line);
  cursor: not-allowed;
}

.confirm-footer button.danger:hover:not(:disabled) {
  background: #a02222;
}

.workspace-picker {
  position: relative;
}

.workspace-select-hidden {
  display: none !important;
}

.workspace-picker-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  width: 100%;
  padding: 6px 10px;
  font-size: 0.9em;
  font-weight: 600;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
}

.workspace-picker-toggle:hover {
  border-color: var(--accent);
}

.workspace-picker-toggle.workspace-picker-multi {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent-strong);
}

.workspace-scope-panel {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  z-index: 200;
  margin-top: 6px;
  min-width: 260px;
  max-width: min(360px, calc(100vw - 32px));
  padding: 10px;
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.workspace-scope-hint {
  margin: 0 0 8px;
  font-size: 0.78em;
  color: var(--muted);
  line-height: 1.35;
}

.workspace-scope-list {
  display: grid;
  gap: 4px;
  max-height: 260px;
  overflow-y: auto;
}

.workspace-scope-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: var(--radius);
  font-size: 0.88em;
}

.workspace-scope-item:hover {
  background: var(--surface-soft);
}

.workspace-scope-item.workspace-scope-active {
  background: var(--surface-strong);
  font-weight: 600;
}

.workspace-scope-checkbox {
  display: inline-flex;
  align-items: center;
  cursor: pointer;
  padding: 4px;
}

.workspace-scope-name {
  flex: 1;
  padding: 4px 6px;
  text-align: left;
  background: transparent;
  border: 0;
  color: inherit;
  font: inherit;
  cursor: pointer;
  overflow-wrap: anywhere;
}

.workspace-scope-name:hover {
  text-decoration: underline;
}

.tier-badge {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 999px;
  font-size: 0.7em;
  font-weight: 600;
  border: 1px solid var(--line);
}

.tier-badge.tier-verified {
  background: color-mix(in srgb, var(--accent) 16%, var(--surface));
  color: var(--accent-strong);
  border-color: var(--accent);
}

.tier-badge.tier-divulgative {
  background: var(--surface-strong);
  color: var(--muted);
  border-color: var(--line-strong);
}

.tier-badge.tier-unverified {
  background: rgba(226, 85, 85, 0.12);
  color: #c53030;
  border-color: #c53030;
}

.tier-badge.tier-none {
  background: transparent;
  color: var(--subtle);
}

.block-result.block-result-ok {
  color: var(--accent-strong);
}

.block-result.block-result-failed {
  color: #e25555;
}

#workspace-form[data-mode="edit"] .field input:disabled::placeholder {
  color: transparent;
}

.workspace-reindex-status {
  margin: 4px 0 0;
  font-size: 0.82em;
  color: var(--accent);
  overflow-wrap: anywhere;
}

.workspace-reindex-status.workspace-reindex-failed {
  color: #e25555;
}

.workspace-reindex-status.workspace-reindex-done {
  color: var(--accent-strong);
}

.settings-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(17, 21, 18, 0.34);
}

.settings-pane {
  position: fixed;
  z-index: 21;
  top: 0;
  left: 0;
  bottom: 0;
  width: min(360px, calc(100vw - 20px));
  padding: var(--space-5);
  overflow: auto;
  border-right: 1px solid var(--line);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.settings-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: var(--space-4);
  margin-bottom: var(--space-5);
}

.settings-list {
  display: grid;
  gap: var(--space-2);
}

.settings-list button {
  justify-content: flex-start;
  width: 100%;
  border: 1px solid var(--line);
  background: var(--surface-soft);
  color: var(--ink);
  text-align: left;
}

.settings-list button:hover {
  border-color: var(--line-strong);
  background: var(--surface-strong);
}

.settings-workspace {
  min-height: 0;
  overflow: auto;
  padding: var(--space-6);
  background: var(--bg);
}

.app.settings-list-open .settings-workspace {
  padding-left: calc(360px + var(--space-6));
}

.settings-workspace-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  margin-bottom: var(--space-5);
}

.settings-section {
  max-width: 980px;
  margin: 0 auto;
}

#settings-form,
#user-form,
#workspace-form,
#mcp-form {
  display: grid;
  gap: var(--space-5);
  padding: var(--space-4);
  border: 0;
  border-radius: 0;
  background: transparent;
}

#workspace-form > label:not(.field),
#mcp-form > label:not(.field) {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-height: 44px;
  color: var(--muted);
  font-size: var(--text-sm);
  font-weight: 750;
}

#workspace-form input[type="checkbox"],
#mcp-form input[type="checkbox"] {
  width: 18px;
  min-width: 18px;
  height: 18px;
  min-height: 18px;
  padding: 0;
}

#settings-form > button,
#user-form > button,
#workspace-form > button,
#mcp-form > button,
#add-mcp-toggle,
fieldset > button[data-save-settings] {
  justify-self: start;
  width: auto;
  min-width: 180px;
}

#add-mcp-toggle {
  margin: 0 0 var(--space-3) var(--space-4);
}

fieldset {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--space-4);
  margin: 0;
  padding: var(--space-5);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

legend {
  padding: 0 var(--space-2);
}

fieldset > button[data-save-settings] {
  grid-column: 1 / -1;
  margin-top: var(--space-1);
}

.full-field {
  grid-column: 1 / -1;
}

fieldset > button {
  align-self: end;
  justify-self: start;
  width: auto;
  min-width: 160px;
}

.block-actions {
  grid-column: 1 / -1;
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  justify-content: flex-start;
}

.block-actions button {
  width: auto;
  min-width: 140px;
}

.add-toggle {
  width: auto;
  min-width: 150px;
  margin-bottom: var(--space-4);
}

#settings-result {
  padding: 0 var(--space-5);
}

.block-result {
  grid-column: 1 / -1;
  min-height: 18px;
}

#usage {
  padding: 0 var(--space-4) var(--space-4);
  overflow-x: auto;
}

.usage-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: var(--text-xs);
}

.usage-table th,
.usage-table td {
  border-top: 1px solid var(--line);
  padding: 4px 6px;
  text-align: right;
  white-space: nowrap;
  font-weight: 500;
}

.usage-table thead th {
  color: var(--muted);
  font-size: var(--text-xs);
  text-transform: uppercase;
  vertical-align: bottom;
}

.usage-table thead th span,
.usage-table td span {
  display: block;
  margin-top: 3px;
  color: var(--subtle);
  font-size: 11px;
  font-weight: 700;
  text-transform: none;
}

.usage-table tbody tr:hover {
  background: var(--surface);
}

.usage-table th:first-child,
.usage-table td:first-child {
  text-align: left;
}

#usage h3 {
  margin: var(--space-5) 0 var(--space-2);
  color: var(--muted);
  font-size: var(--text-sm);
  text-transform: uppercase;
}

.usage-model-table th:nth-child(-n+3),
.usage-model-table td:nth-child(-n+3) {
  text-align: left;
}

.usage-token-table th,
.usage-token-table td {
  font-family: var(--font-mono);
  font-size: 8px;
  padding: 3px 4px;
}

.source-list {
  padding: 0 var(--space-4) var(--space-4);
}

#workspaces .user-list,
#users .user-list,
#mcp-servers .user-list,
.settings-section > .user-list {
  display: grid;
  gap: var(--space-2);
  padding: var(--space-4) 0 0;
}

.settings-section #usage {
  padding: var(--space-4);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-soft);
}

.settings-section:has(#usage) {
  max-width: min(1400px, calc(100vw - 64px));
}

.user-row,
.source-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-4);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

.source-item {
  grid-template-columns: 1fr;
}

.user-row strong,
.source-item strong {
  font-size: var(--text-sm);
  line-height: 1.3;
}

.user-row p,
.source-item p {
  margin: 0;
  color: var(--muted);
  font-size: var(--text-xs);
  line-height: 1.45;
  overflow-wrap: anywhere;
}

.user-actions,
.book-actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
}

.user-actions {
  justify-content: flex-end;
}

.user-actions button {
  min-height: 36px;
  padding: 0 10px;
  font-size: var(--text-xs);
}

.chat {
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto auto;
  min-width: 0;
  background: var(--bg);
}

.settings-workspace:not([hidden]) + .chat {
  display: none;
}

.messages {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  padding: var(--space-6);
  overflow: auto;
}

.message {
  display: flex;
}

.message.user {
  justify-content: flex-end;
}

.bubble {
  max-width: min(900px, 92%);
  padding: 14px var(--space-4);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow-soft);
  line-height: 1.55;
  white-space: pre-wrap;
}

.message.assistant .bubble {
  background: var(--accent-soft);
}

.message.pending .bubble {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  color: var(--muted);
}

.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid var(--line-strong);
  border-top-color: var(--accent);
  border-radius: 999px;
  animation: spin 800ms linear infinite;
}

.message.user .bubble {
  border-color: var(--accent);
  background: var(--accent);
  color: #ffffff;
}

.answer-sources {
  margin-top: var(--space-4);
  padding-top: var(--space-3);
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: var(--text-sm);
  line-height: 1.45;
  white-space: normal;
}

.answer-sources strong {
  display: block;
  margin-bottom: var(--space-2);
  color: var(--ink);
  font-size: var(--text-sm);
}

.answer-sources p {
  margin: 0 0 var(--space-2);
  overflow-wrap: anywhere;
}

.answer-sources a {
  display: inline-flex;
  margin-top: var(--space-1);
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.composer {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-6);
  border-top: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(10px);
}

textarea {
  min-height: 52px;
  max-height: 180px;
  resize: vertical;
  line-height: 1.45;
}

.book-modal {
  width: min(1120px, calc(100vw - 32px));
  height: min(860px, calc(100dvh - 32px));
  max-height: calc(100dvh - 32px);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0;
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow);
  margin: 24px auto auto;
}

.book-modal::backdrop {
  background: rgba(17, 21, 18, 0.46);
}

.book-modal-inner {
  display: grid;
  gap: var(--space-3);
  grid-template-rows: auto auto auto 96px minmax(0, 1fr) auto auto;
  padding: var(--space-4);
  height: 100%;
  min-height: 0;
  overflow: hidden;
}

.book-modal-inner h2 {
  font-size: var(--text-lg);
}

.book-modal header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
}

.book-metadata {
  display: grid;
  grid-template-columns: minmax(220px, 1.35fr) minmax(190px, .95fr) 96px minmax(180px, .9fr);
  gap: var(--space-3);
  align-items: end;
}

.book-modal .field {
  gap: 4px;
  font-size: 11px;
}

.book-modal input,
.book-modal select,
.book-modal button {
  min-height: 38px;
  height: 38px;
  padding-top: 7px;
  padding-bottom: 7px;
  font-size: 14px;
}

.book-actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  align-items: center;
}

.drop-zone {
  display: grid;
  place-items: center;
  border: 1px dashed var(--accent);
  border-radius: var(--radius);
  min-height: 96px;
  padding: var(--space-3);
  text-align: center;
  color: var(--muted);
  font-size: var(--text-sm);
  background: var(--surface-soft);
  cursor: pointer;
  transition: background 120ms ease, border-color 120ms ease;
}

.drop-zone:hover,
.drop-zone:focus-visible {
  background: var(--surface-strong);
  border-color: var(--accent-strong);
  color: var(--ink);
  outline: none;
}

.drop-zone.drag-over {
  border-style: solid;
  background: var(--accent-soft);
}

.file-organizer {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  min-height: 0;
  min-width: 0;
  overflow-y: scroll;
  overflow-x: hidden;
  scrollbar-gutter: stable;
  overscroll-behavior: contain;
  padding: var(--space-3);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-soft);
}

.file-organizer-head {
  display: flex;
  flex: 0 0 auto;
  justify-content: space-between;
  gap: var(--space-3);
  color: var(--muted);
  font-size: var(--text-xs);
}

.image-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  align-content: start;
  gap: var(--space-3);
  min-height: 0;
  max-height: 100%;
  overflow: visible;
  overflow-x: hidden;
  padding-right: var(--space-1);
}

.image-tile {
  position: relative;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  grid-template-rows: auto auto;
  column-gap: var(--space-2);
  row-gap: var(--space-1);
  min-height: 86px;
  padding: var(--space-3);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: grab;
}

.remove-file {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 26px;
  min-width: 26px;
  height: 26px;
  min-height: 26px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface-soft);
  color: var(--muted);
  font-size: 14px;
  line-height: 1;
  box-shadow: none;
  cursor: pointer;
}

.remove-file:hover {
  background: var(--surface-strong);
  color: var(--ink);
}

.image-tile span {
  display: grid;
  grid-row: 1 / span 2;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: var(--radius);
  background: var(--accent-soft);
  color: var(--accent-strong);
  font-size: var(--text-xs);
  font-weight: 850;
}

.image-tile strong {
  display: block;
  margin-bottom: var(--space-1);
  font-size: var(--text-xs);
  line-height: 1.3;
  overflow-wrap: anywhere;
}

.image-tile small {
  color: var(--muted);
}

.empty-organizer {
  display: grid;
  min-height: 100px;
  place-items: center;
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius);
  color: var(--muted);
  font-size: var(--text-sm);
  background: var(--surface);
}

.book-modal-footer {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: var(--space-4);
  padding-top: var(--space-3);
}

.book-modal-footer button + button {
  margin-left: var(--space-3);
}

#references-modal {
  width: min(1180px, calc(100vw - 32px));
  height: min(680px, calc(100dvh - 32px));
}

#references-modal .book-modal-inner {
  grid-template-rows: auto minmax(0, 1fr);
  gap: var(--space-4);
  width: 100%;
}

.references-modal-inner {
  max-width: none;
}

.references-list {
  width: 100%;
  min-height: 0;
  max-height: min(66dvh, 680px);
  overflow-y: scroll;
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.reference-row {
  display: grid;
  grid-template-columns: minmax(180px, 1.8fr) minmax(120px, 1fr) 58px minmax(100px, .9fr) minmax(126px, 1fr) minmax(100px, .9fr) 64px minmax(130px, 1fr) 92px;
  gap: var(--space-3);
  align-items: center;
  padding: var(--space-3);
  border-bottom: 1px solid var(--line);
}

.reference-row:last-child {
  border-bottom: 0;
}

.reference-row strong,
.reference-row span {
  min-width: 0;
  overflow-wrap: anywhere;
}

.reference-download {
  display: inline-block;
  padding: 2px 6px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--accent);
  background: var(--surface);
  font-size: 0.7em;
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
}

.reference-download:hover {
  background: var(--surface-strong);
  border-color: var(--line-strong);
}

.reference-download-unavailable {
  color: var(--subtle);
  font-size: 0.7em;
  white-space: nowrap;
}

.settings-list button {
  font-size: var(--text-base);
  font-weight: 650;
  line-height: 1.15;
  min-height: 42px;
  padding: 0 var(--space-3);
  text-align: left;
  white-space: nowrap;
}

@media (max-width: 1100px) {
  .topbar {
    grid-template-columns: minmax(180px, 1fr) auto;
  }

  .topbar-work {
    grid-column: 1 / -1;
    grid-template-columns: minmax(96px, 120px) minmax(180px, 1fr) minmax(170px, max-content) minmax(170px, max-content);
  }

  .topbar-actions {
    grid-row: 1;
    grid-column: 2;
  }
}

@media (max-width: 760px) {
  .app {
    min-height: 100dvh;
  }

  .topbar,
  .topbar-work,
  .composer,
  fieldset {
    grid-template-columns: 1fr;
  }

  .topbar {
    align-items: stretch;
    padding: var(--space-3);
  }

  .topbar > button,
  .topbar-actions,
  .composer button {
    width: 100%;
  }

  .topbar-actions {
    justify-content: stretch;
  }

  .topbar-actions button {
    width: 100%;
  }

  .book-metadata {
    grid-template-columns: 1fr;
  }

  .user-row {
    grid-template-columns: 1fr;
  }

  .user-actions {
    justify-content: flex-start;
  }

  .settings-pane {
    width: 100vw;
  }

  .app.settings-list-open .settings-workspace {
    padding-left: var(--space-4);
  }

  .messages {
    padding: var(--space-4);
  }

  .composer {
    padding: var(--space-4);
    padding-bottom: calc(var(--space-4) + var(--keyboard-height, 0px));
  }

  .bubble {
    max-width: 100%;
  }
}

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