/* page-dashboard-builder.css — three-pane dashboard layout builder
 * (/dashboards/:id/edit, rendered by js/pages/dashboard-builder.js).
 *
 * Structure ported from the ft-aps form builder (frontend/form-builder.html
 * lines 13-87): palette | canvas | inspector, panes scroll independently,
 * page chrome fixed. Restyled to this app's indigo tokens — accent is
 * var(--color-primary) #4f46e5 (chrome.css), never ft-aps blue.
 *
 * Class prefixes:
 *   .dbx-*  builder chrome (this page only)
 *   .dnd-*  shared drag engine (js/dnd-sortable.js) — the engine sets geometry
 *           inline; these rules only supply colour/opacity per the house spec.
 * Widget internals reuse the view-mode classes from css/page-dashboard.css
 * (.dash-widget, .dash-widget-body, .dash-panel, .dash-widget-chart-wrap) so a
 * widget looks identical in the builder and on the dashboard.
 */

/* ── shell: the builder owns the viewport; only panes scroll ───────── */
#view-dashboard-builder.route-view.active { overflow: hidden; }
#dashboard-builder-root {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.dbx-shell {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

/* ── top bar: name · autosave status · Done ───────────────────────── */
.dbx-topbar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0 0 0.6rem;
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
  flex-wrap: wrap;
}
.dbx-eyebrow {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-muted);
  flex-shrink: 0;
}
.dbx-name {
  font-size: 1.125rem;
  font-weight: 600;
  color: var(--text-primary);
  padding: 4px 8px;
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  min-width: 180px;
  max-width: 460px;
  flex: 1;
  cursor: text;
  transition: background-color 0.12s, border-color 0.12s;
}
.dbx-name:hover { background: var(--bg-secondary); }
.dbx-name:focus {
  outline: 2px solid var(--color-primary);
  outline-offset: 1px;
  background: var(--bg-primary);
}
.dbx-name:empty::before {
  content: attr(data-placeholder);
  color: var(--text-muted);
  font-style: italic;
}
.dbx-status {
  font-size: 0.75rem;
  color: var(--text-muted);
  min-width: 5.5rem;
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.dbx-status.dbx-status-error { color: #c53030; font-weight: 600; }
.dbx-topbar-actions {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  flex-shrink: 0;
}

/* ── three panes ──────────────────────────────────────────────────── */
.dbx-layout {
  flex: 1;
  display: grid;
  grid-template-columns: 220px 1fr 300px;
  min-height: 0;
}
.dbx-pane {
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--bg-secondary);
  padding: 0.85rem;
}
.dbx-palette { border-right: 1px solid var(--border-color); }
.dbx-inspector { border-left: 1px solid var(--border-color); }

.dbx-pane-title,
.dbx-section-title {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-muted);
  margin-bottom: 0.35rem;
}
/* Tight enough that the whole 14-type catalogue clears an ~820px pane. */
.dbx-section-title { margin-top: 0.7rem; }
.dbx-pane-empty {
  margin-top: 1.75rem;
  padding: 0 0.25rem;
  text-align: center;
  font-size: 0.8125rem;
  line-height: 1.5;
  color: var(--text-muted);
}

/* ── palette ──────────────────────────────────────────────────────────
   Compact single-line rows so all 14 widget types fit without scrolling:
   with 3-line descriptions in flow the pane ran ~1590px against ~820px of
   space, hiding half the catalogue. The description is a hover/focus hint
   (.dbx-tip) instead, and rides on the button's aria-label for AT. */
/* Filter box. The catalogue outgrew the pane again as widget types were added,
   so the palette is searchable rather than scrollable. */
.dbx-palette-search { margin-bottom: 0.5rem; }
.dbx-palette-filter {
  width: 100%;
  padding: 5px 8px;
  font: inherit;
  font-size: 0.8125rem;
  line-height: 1.35;
  color: var(--text-primary);
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
}
.dbx-palette-filter::placeholder { color: var(--text-muted); }
.dbx-palette-filter:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 2px rgba(var(--color-primary-rgb), 0.18);
}
.dbx-palette-empty {
  margin: 0.5rem 0 0;
  padding: 0 0.25rem;
  font-size: 0.75rem;
  line-height: 1.45;
  color: var(--text-muted);
}
/* Filtered-out rows keep their DOM node (they stay the element the drag engine
   primed) — these beat .dbx-palette-item's display:flex on specificity, which
   the UA's bare [hidden] rule does not. */
.dbx-palette-item[hidden],
.dbx-pane-title[hidden],
.dbx-section-title[hidden],
.dbx-palette-empty[hidden],
.dbx-palette-hint[hidden] { display: none; }

.dbx-palette-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  min-height: 34px;
  padding: 5px 8px;
  margin-bottom: 3px;
  text-align: left;
  font: inherit;
  color: var(--text-primary);
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  transition: border-color 0.12s, box-shadow 0.12s;
}
.dbx-palette-item:hover {
  border-color: var(--color-primary);
  box-shadow: var(--shadow-sm);
}
.dbx-palette-item:active { cursor: grabbing; }
.dbx-palette-item:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 1px;
}
.dbx-palette-icon {
  flex-shrink: 0;
  width: 20px;
  height: 20px;
  color: var(--color-primary);
  display: flex;
  align-items: center;
  justify-content: center;
}
.dbx-palette-icon svg { width: 17px; height: 17px; display: block; }
.dbx-palette-label {
  min-width: 0;
  font-size: 0.8125rem;
  font-weight: 600;
  line-height: 1.25;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.dbx-palette-hint {
  margin-top: 0.75rem;
  font-size: 0.7rem;
  line-height: 1.45;
  color: var(--text-muted);
}

/* Hover/focus hint. Lives on <body> (position: fixed) because .dbx-palette is
   overflow-y:auto and would clip an in-pane tooltip at the pane edge. */
.dbx-tip {
  position: fixed;
  z-index: 60;
  max-width: 240px;
  padding: 8px 10px;
  border-radius: var(--radius-md);
  background: #1f2937;
  color: #f9fafb;
  font-size: 0.75rem;
  line-height: 1.45;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.18);
  pointer-events: none;
}
.dbx-tip[hidden] { display: none; }

/* ── canvas ───────────────────────────────────────────────────────── */
.dbx-canvas {
  overflow-y: auto;
  overscroll-behavior: contain;
  /* extra top padding = headroom for the first row's edge-straddling chrome */
  padding: 1.5rem 1rem 1rem;
  background: var(--bg-primary);
  min-width: 0;
}
.dbx-canvas-note {
  display: none;
  margin-bottom: 0.75rem;
  padding: 10px 12px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  background: var(--bg-secondary);
  font-size: 0.8125rem;
  line-height: 1.45;
  color: var(--text-muted);
}

/* The dashboard filter bar (Owner / Business line / Clear) sits above the grid,
   INSIDE the scrolling canvas, so it stays with the layout it filters. Its LOOK
   is .dash-filterbar in css/page-dashboard.css — the builder renders the exact
   same row as the dashboard page and the two must never drift; only placement
   belongs here. */
.dbx-filterbar-wrap { min-width: 0; }
.dbx-filterbar-wrap .dash-filterbar { margin-bottom: 0.9rem; }

/* The canvas grid carries BOTH .dash-grid and .dbx-grid: css/page-dashboard.css
   owns the geometry (4 columns, .dash-span-1..4, .dash-rows-1..3, the legacy
   .dash-tall and the 900/600 px collapse) so the builder and the dashboard can
   never drift. Nothing here may
   restate grid-template-columns or grid-column — this stylesheet loads later,
   and a plain rule would beat page-dashboard.css's media queries. */
.dbx-grid { min-height: 140px; }
.dbx-grid .dbx-widget {
  position: relative;
  border-radius: var(--radius-lg);
}

.dbx-widget::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: var(--radius-lg);
  outline: 1px dashed transparent;
  outline-offset: -1px;
  pointer-events: none;
  transition: outline-color 0.12s;
}
.dbx-widget:hover::after { outline-color: rgba(var(--color-primary-rgb), 0.35); }
.dbx-widget.dbx-selected {
  outline: 2px solid var(--color-primary);
  outline-offset: -2px;
}
.dbx-widget.dbx-selected::after { outline-color: transparent; }

/* widget chrome: grab handle + toolbar, revealed on hover/focus.
   It straddles the widget's TOP EDGE rather than sitting inside it — a KPI card
   centres its value, so an inset pill covered the number it is meant to edit.
   The canvas's top padding and the grid's row gap give it the headroom. */
.dbx-widget-chrome {
  position: absolute;
  top: -12px;
  right: 8px;
  z-index: 3;
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 2px;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  box-shadow: var(--shadow-sm);
  opacity: 0;
  transition: opacity 0.12s;
}
.dbx-widget:hover .dbx-widget-chrome,
.dbx-widget.dbx-selected .dbx-widget-chrome,
.dbx-widget-chrome:focus-within { opacity: 1; }

.dbx-handle {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 22px;
  height: 22px;
  padding: 0 3px;
  color: var(--text-muted);
  font-size: 0.85rem;
  line-height: 1;
  border-radius: 4px;
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
.dbx-handle:hover { background: var(--bg-secondary); color: var(--text-primary); }
.dbx-handle:active { cursor: grabbing; }
.dbx-widget-chrome button {
  border: none;
  background: none;
  cursor: pointer;
  min-width: 22px;
  height: 22px;
  padding: 2px 5px;
  font-size: 0.8rem;
  line-height: 1.2;
  color: var(--text-muted);
  border-radius: 4px;
}
.dbx-widget-chrome button:hover:not(:disabled) {
  background: var(--bg-secondary);
  color: var(--text-primary);
}
.dbx-widget-chrome button:disabled { opacity: 0.35; cursor: default; }
/* the selection button (✎) carries the selected state for assistive tech AND
   for sighted users — the card outline alone is not on the control itself */
.dbx-widget-chrome button[aria-pressed="true"] {
  background: rgba(var(--color-primary-rgb), 0.14);
  color: var(--color-primary);
}
.dbx-widget-chrome button:focus-visible,
.dbx-handle:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 1px;
}

/* resize grip on the right edge */
.dbx-grip {
  position: absolute;
  top: 12px;
  bottom: 12px;
  right: -5px;
  width: 10px;
  z-index: 3;
  cursor: col-resize;
  touch-action: none;
  opacity: 0;
  transition: opacity 0.12s;
}
.dbx-grip::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 4px;
  width: 3px;
  height: 34px;
  margin-top: -17px;
  border-radius: 2px;
  background: var(--color-primary);
}
.dbx-widget:hover .dbx-grip,
.dbx-widget.dbx-selected .dbx-grip,
.dbx-grip:focus-visible { opacity: 0.75; }
.dbx-grip:hover { opacity: 1; }

/* empty canvas = drop zone (ft-aps .fb-drop-zone, indigo tint) */
.dbx-empty {
  grid-column: 1 / -1;
  padding: 34px 16px;
  text-align: center;
  font-size: 0.875rem;
  color: var(--text-muted);
  border: 2px dashed var(--border-color);
  border-radius: var(--radius-lg);
  background: rgba(var(--color-primary-rgb), 0.03);
  transition: border-color 0.12s, background-color 0.12s;
}
.dbx-empty.active,
.dbx-empty.dnd-over {
  border-style: solid;
  border-color: var(--color-primary);
  background: rgba(var(--color-primary-rgb), 0.14);
  color: var(--text-primary);
}

/* ── inspector ────────────────────────────────────────────────────── */
.dbx-insp-head {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding-bottom: 0.6rem;
  margin-bottom: 0.75rem;
  border-bottom: 1px solid var(--border-color);
}
.dbx-insp-type {
  flex: 1;
  min-width: 0;
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.dbx-insp-remove {
  flex-shrink: 0;
  font: inherit;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 5px 10px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-color);
  background: var(--bg-primary);
  color: #c53030;
  cursor: pointer;
  transition: background-color 0.12s, border-color 0.12s;
}
.dbx-insp-remove:hover { background: #fff5f5; border-color: #feb2b2; }
.dbx-insp-remove:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 1px;
}
.dbx-insp-form .form-group { margin-bottom: 0.7rem; }
.dbx-insp-form .form-label {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}
.dbx-insp-form .form-input,
.dbx-insp-form .form-select {
  width: 100%;
  font-size: 0.8125rem;
}
.dbx-insp-form .form-input:focus,
.dbx-insp-form .form-select:focus {
  outline: 2px solid var(--color-primary);
  outline-offset: 1px;
}
.dbx-insp-error {
  margin-top: 0.5rem;
  padding: 7px 9px;
  border-radius: var(--radius-md);
  background: var(--error-bg);
  color: var(--error-text);
  font-size: 0.75rem;
  line-height: 1.4;
}

/* size: ONE 4x3 grid picker (width x height), replacing the segmented span
   control + Tall checkbox + the registry's coarse Size select. Cell (c, r) =
   "c columns wide, r rows tall"; the cells are wider than they are tall so the
   filled rectangle reads like the widget it will produce. The picker only
   exists inside .dbx-inspector, which is display:none below 1024 px — the
   whole pane collapses there, so no separate hide rule is needed. */
.dbx-sizegrid {
  display: inline-flex;
  flex-direction: column;
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  background: var(--bg-primary);
}
.dbx-sizegrid-row { display: flex; gap: 4px; }
.dbx-sizecell {
  width: 46px;
  height: 28px;
  padding: 0;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  background: var(--bg-secondary);
  cursor: pointer;
  transition: background-color 0.1s, border-color 0.1s;
}
/* Heights below this widget type's minimum — a chart or list cannot be drawn in
   one row, so the cell is visibly unavailable rather than silently ignored. */
.dbx-sizecell.is-blocked {
  cursor: not-allowed;
  opacity: 0.4;
  background: repeating-linear-gradient(
    -45deg, var(--bg-secondary), var(--bg-secondary) 3px,
    var(--border-color) 3px, var(--border-color) 4px);
}
.dbx-sizecell.is-on {
  background: rgba(var(--color-primary-rgb), 0.16);
  border-color: var(--color-primary);
}
/* a live preview reads stronger than the committed value it is replacing */
.dbx-sizegrid.is-previewing .dbx-sizecell.is-on {
  background: rgba(var(--color-primary-rgb), 0.32);
}
.dbx-sizecell:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 1px;
}
.dbx-size-readout {
  margin-top: 0.45rem;
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
}
.dbx-hint {
  margin-top: 0.4rem;
  font-size: 0.7rem;
  line-height: 1.4;
  color: var(--text-muted);
}

/* ── shared drag engine (js/dnd-sortable.js) ──────────────────────── */
/* The engine owns geometry (it writes position/size inline); these rules only
   supply the house colour + affordances. Never transition the dragged node. */
.dnd-dragging {
  opacity: 0.4;
  transition: none !important;
}
.dnd-indicator {
  z-index: 60;
  min-width: 2px;
  min-height: 2px;
  border-radius: 2px;
  background: var(--color-primary);
  box-shadow: 0 0 0 1px rgba(var(--color-primary-rgb), 0.25);
  pointer-events: none;
}
.dnd-resizing,
.dnd-resizing * { user-select: none !important; -webkit-user-select: none !important; }

/* ── responsive: panes collapse, canvas keeps arrow reordering ────── */
@media (max-width: 1024px) {
  .dbx-layout { grid-template-columns: 1fr; }
  .dbx-palette,
  .dbx-inspector { display: none; }
  .dbx-canvas { padding: 0.75rem 0; }
  .dbx-canvas-note { display: block; }
  .dbx-widget-chrome,
  .dbx-widget .dbx-widget-chrome { opacity: 1; }
  .dbx-handle,
  .dbx-grip { display: none; }
}
/* 900 px / 600 px column collapse lives in css/page-dashboard.css (.dash-grid,
   .dash-span-*, .dash-rows-*, .dash-tall) — the builder inherits it by wearing
   those classes. Row spans are NOT collapsed there: vertical space is not
   constrained on a narrow screen. */
@media (max-width: 600px) {
  .dbx-name { font-size: 1rem; min-width: 140px; }
  /* 1rem inputs on mobile — stops iOS zooming the whole page on focus */
  .dbx-insp-form .form-input,
  .dbx-insp-form .form-select { font-size: 1rem; }
}

/* Duplicate / copy-to actions at the foot of the inspector. The settings are
   the expensive part of a widget, so reusing them is a first-class action. */
.dbx-insp-actions {
    margin-top: 0.9rem; padding-top: 0.9rem;
    border-top: 1px solid var(--border, #e5e7eb);
    display: flex; flex-direction: column; gap: 0.6rem;
}
.dbx-copyto { display: flex; flex-direction: column; gap: 0.35rem; }
.dbx-copyto .form-label { margin: 0; }
.dbx-copyto .btn { align-self: flex-start; }

/* "Fit height to content" — sits under the size grid. Shown greyed rather than
   hidden when the widget is not full width, so the option is discoverable and
   its one condition is legible. */
.dbx-fit-row {
  display: flex; align-items: center; gap: 0.45rem;
  margin: 0.5rem 0 0; font-size: 0.85rem; cursor: pointer;
}
.dbx-fit-row.is-muted { color: var(--text-muted, #9ca3af); cursor: default; }
.dbx-fit-row input { margin: 0; }
.dbx-fit-hint { margin-top: 0.15rem; }
