/* ============================================================================
 * Viewer toolbar system (viewbar strip + ribbon) — P0
 * Extends the existing .viewport-toolbar glass bar; every color comes from
 * the --b2-* tokens so theme swaps re-skin it for free.
 * ========================================================================== */

/* ── view strip additions ─────────────────────────────────────────────── */

.viewport-toolbar.viewbar { max-width: calc(100% - 220px); flex-wrap: nowrap; }

.viewport-toolbar-btn.viewbar-btn {
  width: auto !important;
  min-width: 28px;
  padding: 0 5px !important;
  gap: 1px;
}
.viewbar-btn .tb-chevron {
  width: 10px !important;
  height: 10px !important;
  margin-left: -1px;
  opacity: 0.7;
}
.viewbar-btn.is-open {
  background-color: var(--b2-sunken) !important;
  color: var(--b2-ink) !important;
}
.viewbar-btn:disabled {
  opacity: 0.35;
  cursor: default !important;
}
.viewbar-btn:disabled:hover { background-color: transparent !important; }

.viewbar-sep {
  width: 1px;
  height: 16px;
  background: var(--b2-line);
  margin: 0 3px;
  flex: 0 0 auto;
}

.viewbar-status {
  font-size: 11px;
  line-height: 1;
  color: var(--b2-acc-ink);
  background: var(--b2-acc-soft);
  border-radius: 4px;
  padding: 6px 7px;
  white-space: nowrap;
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── popover menu ─────────────────────────────────────────────────────── */

.viewbar-menu {
  position: absolute;
  min-width: 172px;
  background: var(--b2-surface);
  border: 1px solid var(--b2-line);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(17, 18, 22, 0.10), 0 2px 6px rgba(17, 18, 22, 0.06);
  padding: 4px;
  z-index: 1200;
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.viewbar-menu-item {
  display: flex;
  align-items: center;
  gap: 7px;
  height: 28px;
  padding: 0 9px 0 6px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--b2-ink-2, #3f4350);
  font-size: 12px;
  font-family: inherit;
  cursor: pointer;
  text-align: left;
  white-space: nowrap;
}
.viewbar-menu-item:hover { background: var(--b2-sunken); color: var(--b2-ink); }
.viewbar-menu-item.is-checked { color: var(--b2-acc-ink); }
.viewbar-menu-check {
  width: 14px;
  height: 14px;
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
}
.viewbar-menu-check svg { width: 13px; height: 13px; stroke-width: 2; }
.viewbar-menu-icon { display: inline-flex; }
.viewbar-menu-icon svg { width: 15px; height: 15px; }
.viewbar-menu-label { flex: 1 1 auto; }

/* ── tooltip ──────────────────────────────────────────────────────────── */

.viewbar-tip {
  position: fixed;
  display: none;
  z-index: 3000;
  background: rgba(24, 26, 31, 0.93);
  color: #F4F5F7;
  border-radius: 6px;
  padding: 5px 8px;
  font-size: 11.5px;
  line-height: 1.35;
  pointer-events: none;
  max-width: 260px;
  box-shadow: 0 3px 10px rgba(17, 18, 22, 0.18);
}
.viewbar-tip-hint { color: rgba(244, 245, 247, 0.62); }

/* ── ribbon (P1 CAD ops; renderer ships in P0, hidden while empty) ──────
 * Single 40px row: the theme locks every panel header (sidebar-header,
 * chat-header, file-tree-title, pdf-header) to a 40px band and the ribbon's
 * bottom border must continue that line across the viewer column. Buttons
 * are horizontal icon+label pills (28px tall, viewbar parity). When the
 * column is too narrow the fitter in ribbon.js degrades one tier at a time:
 * .ribbon--compact hides button labels (tooltips carry the names), then
 * .ribbon--minimal drops the group labels; only past that does the strip
 * fall back to horizontal scrolling. */

#viewerRibbon {
  display: none;
  align-items: center;
  gap: 2px;
  height: 40px;
  box-sizing: border-box;
  padding: 0 10px;
  background: var(--b2-bg);
  border-bottom: 1px solid var(--b2-line);
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: thin;
  flex: 0 0 auto;
  position: relative;
  z-index: 50; /* above the empty-state overlay (z 40) */
}

/* Ribbon sits in normal flow at the top of the viewer column — shift the
 * absolute overlays (viewbar, case card, floating DISPLAY rail) below it
 * while it is visible. has-ribbon lives on .trame-viewer-container.
 *
 * The viewbar is LEFT-anchored here (id selector: it must outrank the
 * theme's `.trame-in-content #viewportToolbar { right: 264px }`, which
 * encodes the DISPLAY rail's width and shoved the bar into the rail on
 * narrow columns). Left of the canvas it shares the 12px axis with the
 * ribbon's first group and the op-panel; the max-width reservation keeps
 * it clear of the rail on degenerate columns, scrolling internally instead
 * of overlapping. */
.has-ribbon #viewportToolbar {
  top: 46px !important;
  left: 12px !important;
  right: auto !important;
  max-width: calc(100% - 300px) !important;
  overflow-x: auto;
  scrollbar-width: none;
}
.has-ribbon #viewportToolbar::-webkit-scrollbar { display: none; }
/* Below the viewbar row — the card is left-anchored too (left:16). */
.has-ribbon .vtk-case-card { top: 90px !important; }
/* Resting position only (deliberately NOT !important): the rail's drag
 * handler writes inline top/right which must keep winning. Extra class in
 * the selector outranks the theme's `.trame-in-content .trame-file-list`. */
.trame-viewer-container.has-ribbon .trame-file-list { top: 46px; }
.ribbon-group-label {
  font-size: 10px;
  letter-spacing: 0.08em;
  color: var(--b2-ink-3, #7a7f8c);
  margin: 0 6px 0 8px;
  flex: 0 0 auto;
  user-select: none;
}
/* First label starts 14px from the panel edge — the same text inset as the
 * EXPLORER header (.file-tree-title padding-left) one seam to the left. */
#viewerRibbon .ribbon-group-label:first-child { margin-left: 4px; }
.ribbon-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 28px;
  padding: 0 8px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--b2-ink-2, #3f4350);
  font-family: inherit;
  cursor: pointer;
  flex: 0 0 auto;
  white-space: nowrap;
  transition: background-color 120ms ease-out, color 120ms ease-out;
}
.ribbon-btn:hover { background: var(--b2-sunken); color: var(--b2-ink); }
.ribbon-btn.is-active { background: var(--b2-acc-soft); color: var(--b2-acc-ink); }
.ribbon-btn:disabled { opacity: 0.35; cursor: default; }
.ribbon-btn:disabled:hover { background: transparent; color: var(--b2-ink-2, #3f4350); }
.ribbon-btn-icon { display: inline-flex; flex: 0 0 auto; }
.ribbon-btn-icon svg { width: 16px; height: 16px; display: block; }
.ribbon-btn-label {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  font-size: 11px;
  line-height: 1;
}
.ribbon-btn-label svg { width: 11px; height: 11px; opacity: 0.7; display: block; } /* menu caret */
.ribbon-sep {
  width: 1px;
  height: 20px;
  background: var(--b2-line);
  margin: 0 6px;
  flex: 0 0 auto;
  align-self: center;
}

/* fitter tiers (ribbon.js adds the classes; widest tier that fits wins) */
#viewerRibbon.ribbon--compact .ribbon-btn { padding: 0 6px; }
#viewerRibbon.ribbon--compact .ribbon-btn-label { display: none; }
#viewerRibbon.ribbon--compact .ribbon-btn--menu .ribbon-btn-label { display: inline-flex; }
#viewerRibbon.ribbon--compact .ribbon-btn-label > span { display: none; }
#viewerRibbon.ribbon--minimal .ribbon-group-label { display: none; }
#viewerRibbon.ribbon--minimal .ribbon-sep { margin: 0 4px; }

/* ── in-viewport overlays (box select, measure) ───────────────────────── */

.viewer-marquee {
  position: absolute;
  border: 1.5px dashed var(--b2-acc);
  background: var(--b2-acc-soft);
  z-index: 1150;
  pointer-events: none;
}

.viewer-measure-label {
  position: absolute;
  transform: translate(-50%, -130%);
  background: rgba(24, 26, 31, 0.93);
  color: #F4F5F7;
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 11.5px;
  font-variant-numeric: tabular-nums;
  z-index: 1150;
  pointer-events: none;
  white-space: nowrap;
  box-shadow: 0 3px 10px rgba(17, 18, 22, 0.18);
}

/* ── op panel (P1 CAD parameter card) ─────────────────────────────────── */

.op-panel {
  position: absolute;
  top: 90px;                /* below ribbon (40) + viewbar row (38 + gaps):
                               in narrow viewer columns the right-anchored
                               viewbar reaches into the panel's row and
                               steals its clicks */
  left: 12px;
  width: 268px;
  z-index: 1090;            /* under the toolbar (1100), over the canvas */
  background: var(--b2-surface);
  border: 1px solid var(--b2-line);
  border-radius: 10px;
  box-shadow: 0 12px 32px rgba(17, 18, 22, 0.14), 0 2px 8px rgba(17, 18, 22, 0.08);
  padding: 10px 12px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 12px;
  color: var(--b2-ink-2, #3f4350);
}
.op-panel.is-busy .op-panel-body { opacity: 0.55; }

.op-panel-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--b2-ink);
}
.op-panel-x {
  border: 0;
  background: transparent;
  color: var(--b2-ink-3, #7a7f8c);
  font-size: 17px;
  line-height: 1;
  padding: 0 2px;
  cursor: pointer;
  border-radius: 4px;
}
.op-panel-x:hover { color: var(--b2-ink); background: var(--b2-sunken); }

.op-panel-body { display: flex; flex-direction: column; gap: 6px; }

.op-panel-row {
  display: grid;
  grid-template-columns: 84px minmax(0, 1fr) auto;
  align-items: center;
  gap: 7px;
}
.op-panel-label { font-size: 11.5px; color: var(--b2-ink-3, #7a7f8c); }
.op-panel-input {
  width: 100%;
  min-width: 0;
  padding: 4px 7px;
  border: 1px solid var(--b2-line);
  border-radius: 6px;
  background: var(--b2-surface);
  color: var(--b2-ink);
  font-family: inherit;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}
.op-panel-input:focus { outline: none; border-color: var(--b2-acc); }
.op-panel-unit { font-size: 10.5px; color: var(--b2-ink-3, #7a7f8c); }

.op-panel-status {
  min-height: 15px;
  font-size: 11.5px;
  line-height: 1.35;
  color: var(--b2-ink-3, #7a7f8c);
  white-space: pre-line;      /* check results are one line per file */
  max-height: 180px;
  overflow-y: auto;
}
.op-panel-status.is-error { color: var(--b2-err, #DC2626); }
.op-panel-status.is-ok { color: var(--b2-acc-ink); }

.op-panel-foot { display: flex; justify-content: flex-end; gap: 6px; }
.op-panel-btn {
  padding: 5px 12px;
  border: 1px solid var(--b2-line);
  border-radius: 6px;
  background: var(--b2-surface);
  color: var(--b2-ink-2, #3f4350);
  font-family: inherit;
  font-size: 12px;
  cursor: pointer;
}
.op-panel-btn:hover { background: var(--b2-sunken); color: var(--b2-ink); }
.op-panel-btn:disabled { opacity: 0.5; cursor: default; }
.op-panel-btn-primary {
  background: var(--b2-acc);
  border-color: var(--b2-acc);
  color: #fff;
}
.op-panel-btn-primary:hover { background: var(--b2-acc); color: #fff; filter: brightness(0.94); }

/* ── constructions popover (op list) ──────────────────────────────────── */

.op-list { width: 300px; max-height: 320px; overflow-y: auto; }
.op-list-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 10px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--b2-ink-3, #7a7f8c);
  padding: 4px 8px 3px;
}
.op-list-head > span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.op-list-head > span.is-error { color: var(--b2-err, #DC2626); }
.op-list-head .op-list-undo { text-transform: none; letter-spacing: normal; }
.op-list-empty { padding: 8px; font-size: 11.5px; color: var(--b2-ink-3, #7a7f8c); }
.op-list-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 8px;
  border-radius: 6px;
}
.op-list-row:hover { background: var(--b2-sunken); }
.op-list-text { flex: 1 1 auto; min-width: 0; }
.op-list-name {
  font-size: 12px;
  color: var(--b2-ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.op-list-detail {
  font-size: 11px;
  color: var(--b2-ink-3, #7a7f8c);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.op-list-detail.is-error { color: var(--b2-err, #DC2626); }
/* Dependents warning — long text, must be readable: wrap instead of clip. */
.op-list-detail.is-warn {
  color: var(--b2-warn, #B45309);
  white-space: normal;
}
.op-list-undo { flex: 0 0 auto; padding: 3px 9px; font-size: 11.5px; }

/* History: undone ops stay listed (greyed) with a Restore action. */
.op-list-sep {
  padding: 6px 8px 2px;
  font-size: 9.5px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--b2-ink-3, #7a7f8c);
  border-top: 1px solid var(--b2-line, #e3e5ea);
  margin-top: 4px;
}
.op-list-row.is-tomb .op-list-text { opacity: 0.55; }
.op-list-restored {
  flex: 0 0 auto;
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--b2-ink-3, #7a7f8c);
  border: 1px solid currentColor;
  border-radius: 999px;
  padding: 1px 7px;
}
/* Non-watertight derive warning (derive-time topology audit). */
.op-list-name .op-list-openbody {
  margin-left: 6px;
  font-size: 8.5px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--b2-warn, #B45309);
  border: 1px solid currentColor;
  border-radius: 999px;
  padding: 0 6px;
  vertical-align: 1px;
}
/* Agent-recorded construction marker (scripts/journal_op.py lane). */
.op-list-name .op-list-agent {
  margin-left: 6px;
  font-size: 8.5px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--b2-accent, #2563EB);
  border: 1px solid currentColor;
  border-radius: 999px;
  padding: 0 6px;
  vertical-align: 1px;
}

/* ── commit toast (one-click regret window) ───────────────────────────── */

.op-toast {
  position: absolute;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1200;
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: min(520px, calc(100% - 32px));
  padding: 8px 8px 8px 14px;
  background: var(--b2-surface);
  border: 1px solid var(--b2-line);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(17, 18, 22, 0.10), 0 2px 6px rgba(17, 18, 22, 0.06);
  font-size: 12px;
  color: var(--b2-ink);
}
.op-toast-text { min-width: 0; }
.op-toast-undo { flex: 0 0 auto; padding: 3px 10px; font-size: 11.5px; }
.op-toast-close {
  flex: 0 0 auto;
  border: none;
  background: none;
  cursor: pointer;
  padding: 2px 5px;
  font-size: 14px;
  line-height: 1;
  color: var(--b2-ink-3, #7a7f8c);
}
.op-toast-close:hover { color: var(--b2-ink); }

/* ── superseded badge (scene tree) ────────────────────────────────────── */

/* A derive op replaced this upload for simulation purposes — mark, don't
 * hide: the file stays listed and re-showable, the badge carries intent. */
#sceneTree .scene-count.scene-superseded {
  flex: 0 0 auto;
  color: var(--b2-warn, #B45309);
  border: 1px solid currentColor;
  border-radius: 999px;
  padding: 0 6px;
  font-size: 9px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  opacity: 0.85;
}

/* ── lineage fold (scene tree) ────────────────────────────────────────── */

/* Derive chains collapse to their head; superseded ancestors live in this
 * quiet History group — present when needed, invisible when not. */
#sceneTree .scene-lineage-row {
  opacity: 0.62;
}
#sceneTree .scene-lineage-row:hover { opacity: 1; }
#sceneTree .scene-lineage-row .scene-lineage-label {
  font-size: 10px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

@media (prefers-reduced-motion: reduce) {
  .ribbon-btn, .viewbar-menu-item { transition: none; }
}
