/* ==========================================================================
   Library (documents)
   ========================================================================== */

.library {
  --library-bar-h: 51px;
  display: grid;
  grid-template-columns: 232px 1fr;
  height: 100%;
  overflow: hidden;
}

.library-aside {
  border-right: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* The folder header and the document toolbar sit side by side, so their bottom
   rules have to land on the same line whatever the height of their controls. */
.library-aside-head {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: var(--library-bar-h);
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}

/* Both bars must resolve to exactly --library-bar-h, so their controls are
   pinned to the same height instead of relying on intrinsic sizing. */
.library-aside-head > .btn {
  height: 30px;
  width: 30px;
}

.library-toolbar > .btn,
.library-toolbar > .input,
.library-toolbar > .select {
  height: 30px;
  min-height: 30px;
}

.library-toolbar > .btn-icon {
  width: 30px;
}

.library-aside-head span {
  font-size: 9.5px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink-faint);
}

.tree {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.tree-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 5px 8px;
  border: 0;
  border-radius: var(--radius);
  background: none;
  font-size: var(--fs-md);
  color: var(--ink-soft);
  text-align: left;
  cursor: pointer;
}

.tree-item:hover {
  background: var(--surface-2);
  color: var(--ink);
}

.tree-item.is-active {
  background: var(--accent-soft);
  color: var(--accent-ink);
  font-weight: 600;
}

.tree-item.is-drop-target {
  background: var(--highlight-soft);
  box-shadow: inset 0 0 0 1px var(--highlight);
}

.tree-item svg {
  width: 15px;
  height: 15px;
  flex: none;
  opacity: 0.8;
}

.tree-item-label {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tree-item-count {
  font-size: 10px;
  font-weight: 650;
  color: var(--ink-faint);
  font-variant-numeric: tabular-nums;
}

.tree-actions {
  opacity: 0;
  display: flex;
  gap: 2px;
}

.tree-item:hover .tree-actions {
  opacity: 1;
}

.library-main {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.library-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: var(--library-bar-h);
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-wrap: wrap;
}

.library-filters {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-1);
  flex-wrap: wrap;
  font-size: var(--fs-sm);
}

.filter-label {
  font-size: var(--fs-xs);
  font-weight: 650;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin-right: 2px;
}

.library-body {
  flex: 1;
  overflow: auto;
  background: var(--surface);
}

.selection-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  background: var(--navy-900);
  color: #fff;
  font-size: var(--fs-md);
}

html[data-theme="dark"] .selection-bar {
  background: var(--accent-soft);
  color: var(--ink);
}

.selection-bar .btn {
  background: rgba(255, 255, 255, 0.12);
  border-color: rgba(255, 255, 255, 0.24);
  color: #fff;
}

.selection-bar .btn:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.22);
  border-color: rgba(255, 255, 255, 0.4);
}

html[data-theme="dark"] .selection-bar .btn {
  color: var(--ink);
}

/* -- dropzone ------------------------------------------------------------- */
.dropzone {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 26px;
  margin: 14px;
  border: 1.5px dashed var(--border-strong);
  border-radius: var(--radius-lg);
  background: var(--surface-1);
  text-align: center;
  transition: border-color var(--transition), background var(--transition);
}

.dropzone.is-over {
  border-color: color-mix(in srgb, var(--drop-tint) 60%, transparent);
  background: color-mix(in srgb, var(--drop-tint) 9%, transparent);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--drop-tint) 24%, transparent);
}

.dropzone.is-over .dropzone-icon {
  background: color-mix(in srgb, var(--drop-tint) 20%, transparent);
  color: var(--drop-tint);
}

/* Whole-surface drop target: a translucent, blurred sheet over the library so
   the underlying rows stay readable while dragging. */
.drop-overlay {
  position: absolute;
  inset: 10px;
  z-index: 20;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 26px;
  border: 2px dashed color-mix(in srgb, var(--drop-tint) 55%, transparent);
  border-radius: var(--radius-lg);
  background: linear-gradient(
      to bottom,
      color-mix(in srgb, var(--drop-tint) 9%, transparent),
      color-mix(in srgb, var(--drop-tint) 4%, transparent)
    ),
    color-mix(in srgb, var(--surface) 66%, transparent);
  backdrop-filter: blur(10px) saturate(120%);
  -webkit-backdrop-filter: blur(10px) saturate(120%);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--drop-tint) 22%, transparent),
    0 24px 60px -30px rgba(16, 24, 40, 0.45);
  text-align: center;
  animation: drop-in 160ms ease;
}

@keyframes drop-in {
  from {
    opacity: 0;
    transform: scale(0.99);
  }
}

.drop-overlay .dropzone-icon {
  width: 48px;
  height: 48px;
  background: color-mix(in srgb, var(--drop-tint) 18%, transparent);
  color: var(--drop-tint);
  box-shadow: 0 0 0 6px color-mix(in srgb, var(--drop-tint) 8%, transparent);
}

.drop-overlay .dropzone-icon svg {
  width: 22px;
  height: 22px;
}

.drop-overlay strong {
  font-size: var(--fs-lg);
  font-weight: 640;
  letter-spacing: -0.01em;
}

.drop-overlay p {
  font-size: var(--fs-md);
  color: var(--ink-muted);
}

.dropzone-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent-ink);
}

.dropzone-icon svg {
  width: 19px;
  height: 19px;
}

.dropzone strong {
  font-size: var(--fs-base);
  font-weight: 620;
}

.dropzone p {
  font-size: var(--fs-md);
  color: var(--ink-muted);
}

.upload-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 0 14px 14px;
}

.upload-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  font-size: var(--fs-md);
}

/* ==========================================================================
   Document viewer
   ========================================================================== */

/* The page itself is what the screen is for. The side column keeps its full
   width on large displays but gives room back on laptop-class widths, where
   four columns of chrome would otherwise squeeze the document. */
.viewer {
  display: grid;
  grid-template-columns: minmax(0, 1fr) clamp(304px, 22vw, 384px);
  height: 100%;
  overflow: hidden;
}

.viewer.is-wide {
  grid-template-columns: minmax(0, 1fr) clamp(344px, 25vw, 460px);
}

.viewer-canvas {
  position: relative;
  display: flex;
  flex-direction: column;
  background: var(--surface-3);
  overflow: hidden;
}

.viewer-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-wrap: wrap;
}

.viewer-stage {
  flex: 1;
  overflow: auto;
  padding: 18px;
  display: flex;
  /* `safe` keeps the top-left corner reachable once the page outgrows the
     stage; plain centring pushes the overflow out of scroll range. */
  justify-content: safe center;
  align-items: flex-start;
  cursor: grab;
}

.viewer-stage img {
  -webkit-user-drag: none;
  user-select: none;
}

.viewer-stage.is-panning {
  cursor: grabbing;
  user-select: none;
}

/* While dragging, the page must not steal the pointer back from the stage. */
.viewer-stage.is-panning .bbox {
  cursor: grabbing;
  pointer-events: none;
}

.page-frame {
  position: relative;
  display: inline-block;
  flex: none;
  background: #fff;
  box-shadow: var(--shadow-md);
  border-radius: 2px;
  transform-origin: top center;
}

.page-frame img {
  display: block;
  max-width: 100%;
  height: auto;
}

/* When the frame is driven by the zoom level, the preview must follow it. With
   `max-width: 100%` alone the bitmap stops at its natural resolution and the
   frame keeps growing, leaving a blank white band on the right. */
.page-frame.is-sized img {
  width: 100%;
  max-width: none;
}

.page-frame iframe {
  display: block;
  border: 0;
  width: 900px;
  height: 78vh;
  max-width: 100%;
  background: #fff;
}

.bbox-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.bbox {
  position: absolute;
  border: 1.2px solid var(--navy-900);
  background: color-mix(in srgb, var(--navy-900) 8%, transparent);
  border-radius: 1px;
  pointer-events: auto;
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition);
}

.bbox:hover {
  background: color-mix(in srgb, var(--navy-900) 18%, transparent);
}

.bbox.is-low {
  border-color: var(--orange-700);
  background: color-mix(in srgb, var(--orange-700) 12%, transparent);
  animation: bbox-flash 2.4s ease-in-out infinite;
}

.bbox.is-corrected {
  border-color: var(--success);
  background: color-mix(in srgb, var(--success) 14%, transparent);
  animation: none;
}

.bbox.is-focus {
  border-width: 2px;
  border-color: var(--orange-700);
  background: color-mix(in srgb, var(--orange-700) 24%, transparent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--orange-700) 30%, transparent);
  animation: none;
}

@keyframes bbox-flash {
  0%,
  100% {
    background: color-mix(in srgb, var(--orange-700) 10%, transparent);
  }
  50% {
    background: color-mix(in srgb, var(--orange-700) 24%, transparent);
  }
}

.bbox-tag {
  position: absolute;
  top: -15px;
  left: -1px;
  padding: 0 4px;
  font-size: 8.5px;
  font-weight: 700;
  line-height: 14px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #fff;
  background: var(--navy-900);
  border-radius: 2px 2px 0 0;
  white-space: nowrap;
}

.bbox.is-low .bbox-tag,
.bbox.is-focus .bbox-tag {
  background: var(--orange-700);
}

.viewer-side {
  display: flex;
  flex-direction: column;
  border-left: 1px solid var(--border);
  background: var(--surface);
  overflow: hidden;
}

.viewer-side-tabs {
  display: flex;
  gap: 0;
  border-bottom: 1px solid var(--border);
  background: var(--surface-1);
  padding: 0 6px;
}

.viewer-side-tabs button {
  padding: 9px 10px;
  border: 0;
  border-bottom: 2px solid transparent;
  background: none;
  font-size: var(--fs-sm);
  font-weight: 600;
  color: var(--ink-muted);
  cursor: pointer;
}

.viewer-side-tabs button:hover {
  color: var(--ink);
}

.viewer-side-tabs button.is-active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.viewer-side-body {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
}

.page-strip {
  display: flex;
  gap: 6px;
  padding: 8px 12px;
  border-top: 1px solid var(--border);
  background: var(--surface);
  overflow-x: auto;
}

.page-thumb {
  position: relative;
  flex: none;
  width: 44px;
  height: 58px;
  border: 1px solid var(--border-strong);
  border-radius: 2px;
  background: var(--surface-2) center / cover no-repeat;
  cursor: pointer;
  overflow: hidden;
}

.page-thumb.is-active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}

.page-thumb span {
  position: absolute;
  bottom: 0;
  right: 0;
  padding: 0 3px;
  font-size: 9px;
  font-weight: 700;
  color: #fff;
  background: rgba(0, 0, 0, 0.6);
  border-radius: 2px 0 0 0;
}

.page-thumb em {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--orange-700);
}

/* ==========================================================================
   OCR studio
   ========================================================================== */

/* Settings are an occasional task: they take over the work surface as their
   own tab rather than squeezing a permanent column next to it. */
.ocr-config-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(330px, 1fr));
  gap: 14px;
  align-items: start;
}

.config-section {
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow-xs);
}

.config-section-title {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 10px;
  font-size: 9.5px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink-faint);
}

/* -- inline engine settings ----------------------------------------------- */
.settings-advanced {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-1);
}

.settings-advanced > summary {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 12px;
  border-radius: var(--radius);
  font-size: var(--fs-md);
  font-weight: 600;
  color: var(--ink-soft);
  cursor: pointer;
  list-style: none;
  user-select: none;
}

.settings-advanced > summary::-webkit-details-marker {
  display: none;
}

.settings-advanced > summary::after {
  content: "";
  flex: none;
  margin-left: auto;
  width: 7px;
  height: 7px;
  border-right: 1.6px solid currentColor;
  border-bottom: 1.6px solid currentColor;
  transform: rotate(45deg) translate(-2px, -2px);
  transition: transform var(--transition);
  color: var(--ink-faint);
}

.settings-advanced[open] > summary::after {
  transform: rotate(225deg) translate(-2px, -2px);
}

.settings-advanced > summary:hover {
  background: var(--surface-2);
  color: var(--ink);
}

.settings-advanced > summary svg {
  flex: none;
  color: var(--ink-faint);
}

.settings-advanced-hint {
  font-size: var(--fs-xs);
  font-weight: 500;
  color: var(--ink-faint);
}

.settings-advanced-body {
  padding: 4px 12px 14px;
  border-top: 1px solid var(--border);
}

.engine-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.engine-tile {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
  padding: 12px 13px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-1);
  text-align: left;
  cursor: pointer;
  font: inherit;
  color: inherit;
}

.engine-tile:hover:not(:disabled) {
  border-color: var(--accent-border);
}

.engine-tile.is-active {
  border-color: var(--accent-border);
  background: var(--accent-soft);
  color: var(--accent-ink);
}

.engine-tile:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.engine-tile strong {
  font-size: var(--fs-md);
  font-weight: 620;
}

.engine-tile span {
  font-size: var(--fs-xs);
  color: var(--ink-muted);
  line-height: 1.45;
}

.engine-tile.is-active span {
  color: inherit;
}

.sandbox {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: hidden;
  min-height: 420px;
}

.sandbox-pane {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sandbox-pane + .sandbox-pane {
  border-left: 1px solid var(--border);
}

.sandbox-pane-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-1);
  font-size: var(--fs-sm);
  font-weight: 620;
}

.sandbox-pane-body {
  flex: 1;
  overflow: auto;
  padding: 12px;
  background: var(--surface-3);
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

.sandbox-pane-body.is-plain {
  background: var(--surface);
  display: block;
}

.ocr-text {
  font-family: var(--font-mono);
  font-size: var(--fs-sm);
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--ink-soft);
}

.json-view {
  font-family: var(--font-mono);
  font-size: var(--fs-sm);
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
  padding: 10px;
  background: var(--surface-inset);
  border: 1px solid var(--border-faint);
  border-radius: var(--radius);
  color: var(--ink-soft);
  max-height: 340px;
  overflow: auto;
}

/* -- live batch monitor --------------------------------------------------- */
.run-monitor {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 12px;
}

.run-stage {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.run-ring {
  position: relative;
  width: 54px;
  height: 54px;
  flex: none;
}

.run-ring svg {
  transform: rotate(-90deg);
}

.run-ring-value {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: var(--fs-sm);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.run-feed {
  max-height: 260px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.run-feed-row {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 7px 12px;
  border-bottom: 1px solid var(--border-faint);
  font-size: var(--fs-md);
  animation: feed-in 200ms ease;
}

@keyframes feed-in {
  from {
    opacity: 0;
    transform: translateY(-4px);
  }
}

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

.scan-beam {
  position: relative;
  height: 3px;
  border-radius: 3px;
  background: var(--surface-3);
  overflow: hidden;
}

.scan-beam::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  width: 34%;
  border-radius: 3px;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  animation: beam 1.25s ease-in-out infinite;
}

@keyframes beam {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(320%);
  }
}

/* ==========================================================================
   Review (human in the loop)
   ========================================================================== */

.review {
  display: grid;
  grid-template-columns: clamp(216px, 15vw, 268px) minmax(0, 1fr);
  height: 100%;
  overflow: hidden;
}

.review-queue {
  border-right: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.review-queue-head {
  padding: 11px 12px;
  border-bottom: 1px solid var(--border);
}

.review-queue-list {
  flex: 1;
  overflow-y: auto;
}

.review-queue-item {
  display: block;
  width: 100%;
  padding: 9px 12px;
  border: 0;
  border-bottom: 1px solid var(--border-faint);
  border-left: 2px solid transparent;
  background: none;
  text-align: left;
  cursor: pointer;
}

.review-queue-item:hover {
  background: var(--surface-1);
}

.review-queue-item.is-active {
  background: var(--accent-soft);
  border-left-color: var(--accent);
}

.review-queue-item strong {
  display: block;
  font-size: var(--fs-md);
  font-weight: 570;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.review-queue-item .row {
  margin-top: 4px;
  gap: 6px;
}

.correction-item {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 8px;
  overflow: hidden;
  background: var(--surface);
}

.correction-item.is-pickable {
  cursor: pointer;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.correction-item.is-focus {
  border-color: var(--orange-700);
  box-shadow: 0 0 0 3px var(--highlight-soft);
}

.correction-item.is-done {
  border-color: color-mix(in srgb, var(--success) 40%, var(--border));
}

.correction-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  background: var(--surface-1);
  border-bottom: 1px solid var(--border-faint);
  font-size: var(--fs-xs);
}

.correction-body {
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.correction-original {
  font-family: var(--font-mono);
  font-size: var(--fs-sm);
  color: var(--ink-muted);
  padding: 5px 7px;
  background: var(--surface-inset);
  border-radius: var(--radius-sm);
  word-break: break-word;
}

.correction-input {
  font-family: var(--font-mono);
  font-size: var(--fs-sm);
  min-height: 30px;
  padding: 5px 8px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  width: 100%;
  resize: vertical;
}

.correction-input:focus {
  outline: none;
  border-color: var(--orange-700);
  box-shadow: 0 0 0 3px var(--highlight-soft);
}

.word-cloud {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.word-pill {
  padding: 1px 6px;
  border-radius: 3px;
  font-family: var(--font-mono);
  font-size: var(--fs-xs);
  background: var(--warning-soft);
  color: var(--warning);
  border: 1px solid color-mix(in srgb, var(--warning) 30%, transparent);
}

/* ==========================================================================
   Classification
   ========================================================================== */

.rule-card {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  padding: 11px 13px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.rule-card.is-inactive {
  opacity: 0.6;
}

.rule-card h4 {
  font-size: var(--fs-md);
  font-weight: 620;
  margin: 0;
}

.link-row {
  display: grid;
  grid-template-columns: 1fr 90px 1fr;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  border-bottom: 1px solid var(--border-faint);
  font-size: var(--fs-md);
}

.link-connector {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  color: var(--ink-faint);
}

.link-connector-line {
  width: 100%;
  height: 1px;
  background: var(--border-strong);
  position: relative;
}

.link-connector-line::before,
.link-connector-line::after {
  content: "";
  position: absolute;
  top: -2px;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--accent);
}

.link-connector-line::before {
  left: 0;
}

.link-connector-line::after {
  right: 0;
}

/* ==========================================================================
   RAG workspace
   ========================================================================== */

.rag-space {
  position: relative;
  display: grid;
  grid-template-columns: 258px 1fr auto;
  height: 100%;
  overflow: hidden;
  background: var(--surface);
}

.rag-space.has-panel {
  grid-template-columns: 258px 1fr auto 360px;
}

.rag-space.has-settings {
  grid-template-columns: 258px 360px 1fr auto;
}

/* A cited source is read next to the thread, not over it: the conversation
   keeps its own column and stays usable while the passage is checked. Three
   tracks only, since the corpus pull-tab steps aside in this state. */
.rag-space.has-source {
  grid-template-columns: 258px minmax(0, 1fr) min(46vw, 560px);
}

.rag-aside {
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  background: var(--surface-1);
  overflow: hidden;
}

.rag-aside-brand {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 72px;
  padding: 0 12px;
  border-bottom: 1px solid var(--border);
  overflow: hidden;
}

.rag-brand-button {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 6px 10px;
  border: 0;
  border-radius: var(--radius);
  background: none;
  cursor: pointer;
  transition: background var(--transition);
}

.rag-brand-button:hover {
  background: var(--surface-2);
}

.rag-account {
  border-top: 1px solid var(--border);
  padding: 8px 10px 10px;
  background: var(--surface-1);
}

.rag-account .account-identity:hover {
  background: var(--surface-2);
}

/* The pull tab is the only way into the corpus panel, so it advertises itself:
   icon, upright label and a chevron that flips with the panel. */
.rag-corpus-tab {
  align-self: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 9px;
  padding: 15px 9px;
  border: 1px solid var(--border);
  border-right: 0;
  border-radius: 10px 0 0 10px;
  background: var(--surface-1);
  color: var(--ink-soft);
  cursor: pointer;
  box-shadow: -3px 0 10px -6px rgba(16, 24, 40, 0.35);
  transition: background var(--transition), color var(--transition), border-color var(--transition);
}

.rag-corpus-tab-label {
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  font-size: var(--fs-xs);
  font-weight: 650;
  letter-spacing: 0.09em;
  text-transform: uppercase;
}

/* The source drawer already occupies the right edge: the corpus pull-tab would
   sit between the thread and the cited page and add nothing useful. */
.rag-space.has-source .rag-corpus-tab {
  display: none;
}

.rag-corpus-tab-icon,
.rag-corpus-tab-chevron {
  display: flex;
  color: var(--ink-faint);
}

.rag-corpus-tab:hover,
.rag-corpus-tab.is-open {
  background: var(--accent-soft);
  color: var(--accent-ink);
  border-color: var(--accent-border);
}

.rag-corpus-tab:hover .rag-corpus-tab-icon,
.rag-corpus-tab:hover .rag-corpus-tab-chevron,
.rag-corpus-tab.is-open .rag-corpus-tab-icon,
.rag-corpus-tab.is-open .rag-corpus-tab-chevron {
  color: inherit;
}

.rag-settings {
  border-right: 1px solid var(--border);
  background: var(--surface-1);
  overflow-y: auto;
}

.corpus-all {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  cursor: pointer;
}

.corpus-all:not(button) {
  cursor: default;
}

button.corpus-all:hover {
  border-color: var(--accent-border);
}

.corpus-all.is-active {
  background: var(--accent-soft);
  border-color: var(--accent-border);
  color: var(--accent-ink);
}

.scope-list-tall {
  max-height: calc(100vh - 220px);
}

.rag-aside-brand .brand-logo {
  height: 30px;
}

.rag-aside-head {
  padding: 11px 12px;
}

.conv-list {
  flex: 1;
  overflow-y: auto;
  padding: 6px;
}

.conv-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  width: 100%;
  padding: 7px 9px;
  border: 0;
  border-radius: var(--radius);
  background: none;
  text-align: left;
  cursor: pointer;
  color: var(--ink-soft);
}

.conv-item:hover {
  background: var(--surface-2);
}

.conv-item.is-active {
  background: var(--accent-soft);
  color: var(--accent-ink);
}

.conv-item-row {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.conv-item strong {
  flex: 1;
  min-width: 0;
  font-size: var(--fs-md);
  font-weight: 570;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conv-item .xs {
  color: var(--ink-faint);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conv-item-actions {
  flex: none;
  display: flex;
  gap: 2px;
  opacity: 0;
  pointer-events: none;
}

.conv-item:hover .conv-item-actions,
.conv-item.is-active .conv-item-actions,
.conv-item:focus-within .conv-item-actions {
  opacity: 1;
  pointer-events: auto;
}

.chat {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* The workspace reads as one continuous sheet: the controls float over the top
   of the conversation instead of sitting in a chrome bar. */
.chat-head {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 10px 48px 6px 14px;
  background: var(--surface);
}

.rag-head-tools {
  position: absolute;
  top: 10px;
  right: 9px;
  z-index: 6;
  display: flex;
  align-items: center;
  gap: 4px;
}

/* Stay clear of whichever panel is deployed on the right edge. */
.rag-space.has-panel .rag-head-tools {
  right: 369px;
}

.rag-space.has-source .rag-head-tools {
  right: calc(min(46vw, 560px) + 9px);
}

.chat-thread {
  flex: 1;
  overflow-y: auto;
  padding: 16px 0 10px;
  background: var(--surface);
}

.chat-inner {
  max-width: 820px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.msg {
  display: flex;
  gap: 11px;
  animation: msg-in 220ms ease;
}

@keyframes msg-in {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
}

.msg-avatar {
  flex: none;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.msg-avatar.is-user {
  background: var(--surface-3);
  color: var(--ink-soft);
}

.msg-avatar.is-bot {
  background: var(--navy-100);
  color: var(--navy-900);
}

/* navy-on-navy is illegible once the palette flips. */
html[data-theme="dark"] .msg-avatar.is-bot {
  background: var(--accent-soft);
  color: var(--accent-ink);
}

html[data-theme="dark"] .msg-avatar.is-user {
  background: var(--surface-3);
  color: var(--ink);
}

html[data-theme="dark"] .source-pill {
  background: var(--surface-2);
  border-color: var(--border-strong);
  color: var(--ink);
}

.conv-item.is-active .btn-ghost {
  color: var(--accent-ink);
}

.msg-body {
  flex: 1;
  min-width: 0;
}

.msg-meta {
  display: flex;
  align-items: center;
  gap: 7px;
  margin-bottom: 4px;
  font-size: var(--fs-xs);
  color: var(--ink-faint);
}

.msg-meta strong {
  font-size: var(--fs-sm);
  font-weight: 650;
  color: var(--ink);
}

.msg-text {
  font-size: var(--fs-base);
  line-height: 1.65;
  color: var(--ink);
  white-space: pre-wrap;
  word-break: break-word;
}

.msg-text.msg-md {
  white-space: normal;
}

.msg-md > *:first-child {
  margin-top: 0;
}

.msg-md > *:last-child {
  margin-bottom: 0;
}

.msg-md p,
.msg-md ul,
.msg-md ol,
.msg-md blockquote,
.msg-md pre {
  margin: 0.55em 0;
}

.msg-md h1,
.msg-md h2,
.msg-md h3,
.msg-md h4 {
  margin: 0.85em 0 0.35em;
  font-weight: 700;
  line-height: 1.3;
  color: var(--ink);
}

.msg-md h1 { font-size: 1.2em; }
.msg-md h2 { font-size: 1.08em; }
.msg-md h3,
.msg-md h4 { font-size: 1em; }

.msg-md ul,
.msg-md ol {
  padding-left: 1.3em;
}

.msg-md li + li {
  margin-top: 0.2em;
}

.msg-md blockquote {
  margin-left: 0;
  padding: 8px 12px;
  border-left: 3px solid var(--highlight);
  background: var(--surface-2);
  border-radius: 0 6px 6px 0;
  color: var(--ink-soft);
}

.msg-md strong {
  font-weight: 750;
}

.msg-md em {
  font-style: italic;
}

.msg-text code,
.msg-md code {
  font-family: var(--font-mono);
  font-size: 0.9em;
  padding: 1px 4px;
  background: var(--surface-2);
  border-radius: 3px;
}

.msg-md pre {
  overflow-x: auto;
  padding: 10px 12px;
  background: var(--surface-2);
  border-radius: 6px;
}

.msg-md pre code {
  padding: 0;
  background: none;
}

.msg-md .katex-display {
  margin: 0.45em 0;
  padding: 0.65em 0.15em 0.45em;
  overflow-x: auto;
  overflow-y: visible;
}

.msg-md .katex-display > .katex {
  overflow: visible;
  padding-top: 0.12em;
}

.msg-md .md-math-fallback {
  font-family: var(--font-mono);
  font-size: 0.95em;
}

.msg-md .md-table-wrap {
  width: 100%;
  overflow-x: auto;
}

.msg-md table {
  width: 100%;
}

.msg-md table {
  border-collapse: collapse;
  margin: 0.7em 0;
  font-size: 0.95em;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.msg-md th,
.msg-md td {
  padding: 7px 10px;
  border: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

.msg-md thead th {
  background: var(--surface-2);
  font-weight: 700;
  color: var(--ink);
}

.msg-md tbody tr:nth-child(even) {
  background: var(--surface-1);
}

.settings-inline {
  padding-top: 12px;
  margin-top: 4px;
  border-top: 1px solid var(--border);
}

.msg-sources {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
}

.source-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 8px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--surface-1);
  font-size: var(--fs-xs);
  font-weight: 550;
  color: var(--ink-soft);
  cursor: pointer;
}

.source-pill:hover:not(:disabled) {
  border-color: var(--highlight);
  color: var(--highlight);
}

.source-pill.is-muted {
  opacity: 0.6;
  cursor: not-allowed;
}

.source-pill.is-active {
  border-color: var(--highlight);
  background: var(--highlight-soft);
  color: var(--highlight);
}

/* -- cited source panel --------------------------------------------------- */
.rag-source {
  display: flex;
  flex-direction: column;
  min-width: 0;
  border-left: 1px solid var(--border);
  background: var(--surface-1);
  overflow: hidden;
}

.source-head {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 12px 12px 12px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.source-head-icon {
  display: flex;
  flex: none;
  width: 28px;
  height: 28px;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  background: var(--accent-soft);
  color: var(--highlight);
}

.source-head-text {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
  flex: 1;
}

.source-head-text strong {
  font-size: var(--fs-md);
  font-weight: 620;
}

.source-tabs {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.source-tabs .segmented {
  width: 100%;
}

.source-tabs .segmented button {
  flex: 1;
  height: 28px;
}

.source-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

.source-pad {
  padding: 16px 14px;
}

.source-hint {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 0;
  padding: 9px 14px;
  font-size: var(--fs-xs);
  color: var(--ink-muted);
  background: var(--surface-1);
  border-bottom: 1px solid var(--border-faint);
}

.source-hint svg {
  flex: none;
  color: var(--highlight);
}

.source-page {
  display: flex;
  flex-direction: column;
}

.source-page-frame {
  position: relative;
  align-self: center;
  max-width: calc(100% - 28px);
  margin: 14px;
  border-radius: 4px;
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}

.source-page-frame img {
  display: block;
  max-width: 100%;
  border-radius: 4px;
}

.source-page-marks {
  position: absolute;
  inset: 0;
}

/* The passage is pointed at rather than boxed in: a wash plus a firm underline
   reads as "here", where a full border would read as a detected zone. */
.source-page-mark {
  position: absolute;
  background: color-mix(in srgb, var(--highlight) 16%, transparent);
  border-bottom: 2px solid var(--highlight);
  border-radius: 2px;
  pointer-events: none;
}

.source-text-view {
  display: flex;
  flex-direction: column;
}

.source-text {
  padding: 14px;
  font-size: var(--fs-md);
  line-height: 1.7;
  color: var(--ink-soft);
  white-space: pre-wrap;
  word-break: break-word;
}

.source-mark {
  padding: 1px 0;
  border-bottom: 2px solid var(--highlight);
  background: color-mix(in srgb, var(--highlight) 16%, transparent);
  color: var(--ink);
  font-weight: 550;
}

.source-foot {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 12px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.source-pager {
  display: flex;
  align-items: center;
  gap: 2px;
  color: var(--ink-muted);
}

.source-pill svg {
  width: 11px;
  height: 11px;
  color: var(--highlight);
}

.typing {
  display: inline-flex;
  gap: 3px;
  align-items: center;
  height: 20px;
}

.typing i {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--ink-faint);
  animation: typing 1.1s ease-in-out infinite;
}

.typing i:nth-child(2) {
  animation-delay: 0.16s;
}

.typing i:nth-child(3) {
  animation-delay: 0.32s;
}

@keyframes typing {
  0%,
  60%,
  100% {
    transform: translateY(0);
    opacity: 0.4;
  }
  30% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

.composer {
  background: var(--surface);
  padding: 6px 20px 16px;
}

.composer-inner {
  max-width: 820px;
  margin: 0 auto;
}

.composer-box {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 9px 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  background: var(--surface);
  transition: border-color var(--transition), box-shadow var(--transition);
}

.composer-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.composer-actions .select {
  width: auto;
  min-width: 152px;
  min-height: 28px;
  padding: 3px 26px 3px 9px;
  font-size: var(--fs-sm);
  background-color: var(--surface-1);
  border-color: var(--border);
}

.composer-hint {
  margin-left: auto;
  font-size: var(--fs-xs);
  color: var(--ink-faint);
}

.composer-box:focus-within {
  border-color: var(--accent-border);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.composer-box textarea {
  flex: 1;
  border: 0;
  background: none;
  resize: none;
  max-height: 200px;
  min-height: 24px;
  font-size: var(--fs-base);
  line-height: 1.55;
  padding: 2px 2px 0;
  color: var(--ink);
}

.composer-box textarea:focus {
  outline: none;
}

.rag-panel {
  border-left: 1px solid var(--border);
  background: var(--surface-1);
  overflow-y: auto;
}

.rag-panel-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 13px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.rag-panel-head h3 {
  margin: 0;
  font-size: var(--fs-md);
  font-weight: 640;
}

.rag-panel-head p {
  margin: 2px 0 0;
  font-size: var(--fs-xs);
  color: var(--ink-muted);
}

.scope-list {
  max-height: 288px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.scope-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border-faint);
  font-size: var(--fs-md);
  cursor: pointer;
}

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

.scope-row:hover {
  background: var(--surface-1);
}

.scope-row input {
  accent-color: var(--navy-900);
  flex: none;
}

.passage {
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  margin-bottom: 8px;
}

.passage-text {
  font-size: var(--fs-sm);
  line-height: 1.6;
  color: var(--ink-soft);
  max-height: 110px;
  overflow: hidden;
  position: relative;
}

/* ==========================================================================
   Settings & logs
   ========================================================================== */

.settings-layout {
  display: grid;
  grid-template-columns: 208px 1fr;
  gap: 18px;
  align-items: start;
}

.settings-nav {
  position: sticky;
  top: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.settings-nav button {
  padding: 7px 10px;
  border: 0;
  border-radius: var(--radius);
  background: none;
  text-align: left;
  font-size: var(--fs-md);
  font-weight: 550;
  color: var(--ink-soft);
  cursor: pointer;
}

.settings-nav button:hover {
  background: var(--surface-3);
}

.settings-nav button.is-active {
  background: var(--accent-soft);
  color: var(--accent-ink);
  font-weight: 620;
}

.kv-list {
  display: flex;
  flex-direction: column;
}

.kv-row {
  display: grid;
  grid-template-columns: 190px 1fr;
  gap: 12px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border-faint);
  font-size: var(--fs-md);
}

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

.kv-key {
  color: var(--ink-muted);
}

.activity-row {
  display: flex;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border-faint);
  font-size: var(--fs-md);
}

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

.activity-dot {
  flex: none;
  width: 6px;
  height: 6px;
  margin-top: 7px;
  border-radius: 50%;
  background: var(--ink-faint);
}

.activity-dot.is-success {
  background: var(--success);
}

.activity-dot.is-warning {
  background: var(--warning);
}

.activity-dot.is-error {
  background: var(--danger);
}

/* ==========================================================================
   Responsive
   ========================================================================== */

@media (max-width: 1280px) {
  .viewer,
  .viewer.is-wide {
    grid-template-columns: 1fr 340px;
  }

  .rag-space.has-panel {
    grid-template-columns: 220px 1fr auto 300px;
  }

  .rag-space.has-settings {
    grid-template-columns: 220px 300px 1fr auto;
  }
}

@media (max-width: 1080px) {
  .shell {
    --sidebar-w: 60px;
  }

  .shell .nav-item-label,
  .shell .nav-count,
  .shell .brand-text {
    display: none;
  }

  .library,
  .review,
  .viewer,
  .rag-space,
  .rag-space.has-panel,
  .settings-layout {
    grid-template-columns: 1fr;
  }

  .sandbox {
    grid-template-columns: 1fr;
  }

  .sandbox-pane + .sandbox-pane {
    border-left: 0;
    border-top: 1px solid var(--border);
  }

  .topbar-search {
    width: 160px;
  }
}
