/* Configurable dashboard: toolbar, grid, edit mode, panel, new widgets. Prefix .xr-dtb / .xr-dgrid / .xr-dash / .xr-dpanel / .xr-dwidget.
   Dashboard v2 (2026-11-01): one tool bar, framed cards with a title strip (no overlay on the widget), column guides,
   a placeholder while dragging, bigger controls for touch. Theme tokens only (light and dark). */

.xr-dash-body {
  display: flex;
  flex-direction: column;
  gap: var(--sp-3);
}

/* ---------- tool bar ---------- */

.xr-dash-toolbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--sp-2) var(--sp-3);
  min-height: 32px;
}

.xr-dash-toolbar.is-editing {
  padding: var(--sp-2) var(--sp-3);
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  animation: xr-dtb-in 0.18s ease-out both;
}

.xr-dtb-group {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  min-width: 0;
}

.xr-dtb-lead {
  margin-right: var(--sp-2);
}

.xr-dtb-badge {
  display: inline-flex;
  align-items: center;
  height: 24px;
  padding: 0 var(--sp-2);
  font-size: var(--fs-label);
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--accent);
  background: var(--accent-100);
  border-radius: 999px;
}

.xr-dtb-spacer {
  flex: 1 1 var(--sp-4);
}

/* One size for every tool: the global `button` rule varies with context. */
.xr-dash-toolbar .xr-dtb-btn {
  box-sizing: border-box;
  height: 32px;
  padding: 0 var(--sp-3);
  gap: 6px;
  font-size: var(--fs-body);
  font-weight: 600;
  white-space: nowrap;
  color: var(--text-primary);
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
}

.xr-dash-toolbar .xr-dtb-btn:not(:disabled):hover {
  background: var(--bg-hover);
  border-color: var(--border-strong);
}

.xr-dash-toolbar .xr-dtb-quiet {
  color: var(--text-secondary);
  background: transparent;
  border-color: transparent;
  font-weight: 500;
}

.xr-dash-toolbar .xr-dtb-primary {
  color: var(--text-inverse);
  background: var(--accent);
  border-color: var(--accent);
}

.xr-dash-toolbar .xr-dtb-primary:not(:disabled):hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

/* Undo / Redo as one segmented control */
.xr-dtb-seg {
  gap: 0;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
  overflow: hidden;
}

.xr-dash-toolbar .xr-dtb-seg .xr-dtb-btn {
  border: 0;
  border-radius: 0;
}

.xr-dash-toolbar .xr-dtb-seg .xr-dtb-btn + .xr-dtb-btn {
  border-left: 1px solid var(--border-default);
}

.xr-dtb-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.xr-dtb-icon {
  width: 14px;
  height: 14px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.xr-dash-status {
  font-size: var(--fs-label);
  color: var(--text-secondary);
  white-space: nowrap;
}

@keyframes xr-dtb-in {
  from {
    opacity: 0;
    transform: translateY(-4px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

/* ---------- layout: grid + panel ---------- */

.xr-dash-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--sp-4);
  align-items: start;
}

.xr-dash-layout.xr-dash-with-panel {
  grid-template-columns: minmax(0, 1fr) 300px;
}

@media (max-width: 1199px) {
  .xr-dash-layout.xr-dash-with-panel {
    grid-template-columns: minmax(0, 1fr);
  }
}

.xr-dash-main {
  min-width: 0;
}

/* Announcements for screen readers only. */
.xr-dash-live {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* The Customize gear, right of the "Dashboard" title. */
.xr-dash-titlerow {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
}

.xr-dash-gear {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  padding: 0;
  color: var(--text-secondary);
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  cursor: pointer;
}

.xr-dash-gear:hover {
  color: var(--text-primary);
  background: var(--bg-hover);
  border-color: var(--border-default);
}

.xr-dash-gear:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* ---------- grid ---------- */

.xr-dgrid {
  --cols: 12;
  --row-h: 72px;
  --gap: 12px;
  --bar-h: 34px;
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  grid-auto-rows: var(--row-h);
  gap: var(--gap);
  position: relative;
}

/* Edit mode: the 12 (or 8) columns are drawn as light stripes the cards snap to, with a hairline between the rows;
   one spare row at the bottom is a place to drop a card. The stripes use the same `--gap` as the grid, so a card edge
   always lies on a stripe edge. */
.xr-dgrid-edit {
  --guide: color-mix(in srgb, var(--accent) 6%, transparent);
  --guide-line: color-mix(in srgb, var(--accent) 16%, transparent);
  padding-bottom: calc(var(--row-h) + var(--gap));
  background-image:
    linear-gradient(to right, var(--guide) calc(100% - var(--gap)), transparent calc(100% - var(--gap))),
    linear-gradient(
      to bottom,
      transparent calc(100% - var(--gap) / 2 - 0.5px),
      var(--guide-line) calc(100% - var(--gap) / 2 - 0.5px),
      var(--guide-line) calc(100% - var(--gap) / 2 + 0.5px),
      transparent calc(100% - var(--gap) / 2 + 0.5px)
    );
  background-size: calc((100% + var(--gap)) / var(--cols)) 100%, 100% calc(var(--row-h) + var(--gap));
  background-repeat: repeat-x, repeat-y;
  background-position: 0 0, 0 0;
  border-radius: var(--radius-md);
}

.xr-dgrid-item {
  position: relative;
  container-type: inline-size;
  container-name: dgitem;
  min-width: 0;
  min-height: 0;
  transition: transform 0.15s ease;
}

/* The cell is fixed: a legacy card scrolls inside instead of growing. */
.xr-dgrid-body {
  height: 100%;
  overflow: auto;
  min-width: 0;
}

.xr-dgrid-body > .xr-dashboard-card {
  min-height: 100%;
  box-sizing: border-box;
}


/* Edit mode: the card is a frame (border, radius, surface) with a title strip on top and the widget below it.
   Nothing is laid over the widget, so no header, switch or "details" link is covered. */
.xr-dgrid-edit .xr-dgrid-item {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  transition: border-color 0.15s ease, box-shadow 0.15s ease, opacity 0.15s ease;
  animation: xr-dgrid-in 0.18s ease-out both;
}

.xr-dgrid-edit .xr-dgrid-item:hover {
  border-color: var(--border-strong);
}

.xr-dgrid-edit .xr-dgrid-body {
  flex: 1 1 auto;
  height: auto;
  min-height: 0;
  overflow: hidden; /* arranging, not reading: the tail is cut, resize the card to see more */
  pointer-events: none;
}

/* the widget's own card frame would double the border of the edit frame */
.xr-dgrid-edit .xr-dgrid-body > .xr-dashboard-card,
.xr-dgrid-edit .xr-dgrid-body > .xr-dw-card {
  min-height: 0;
  height: 100%;
  box-sizing: border-box;
  background: transparent;
  border: 0;
  border-radius: 0;
  box-shadow: none;
}

.xr-dgrid-edit .xr-dgrid-item.xr-dgrid-selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent), var(--shadow-sm);
}

.xr-dgrid-edit .xr-dgrid-item.xr-dgrid-grabbed {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent), var(--shadow-md);
}

.xr-dgrid-edit .xr-dgrid-item.xr-dgrid-dragging {
  opacity: 0.92;
  box-shadow: var(--shadow-md);
  cursor: grabbing;
}

/* Where the card lands. Behind the dragged card, over the others. */
.xr-dgrid-ghost {
  z-index: 1;
  background: color-mix(in srgb, var(--accent) 10%, transparent);
  border: 2px dashed var(--accent);
  border-radius: var(--radius-md);
  pointer-events: none;
  animation: xr-ghost-in 0.12s ease-out both;
}

@keyframes xr-dgrid-in {
  from {
    opacity: 0;
    transform: scale(0.985);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

@keyframes xr-ghost-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

/* Clicks in edit mode select the widget instead of following links. */
.xr-dgrid-shield {
  position: absolute;
  inset: 0;
  z-index: 2;
  cursor: pointer;
}

/* The title strip: grip + title (the move handle), settings, remove. A fixed height for every card. */
.xr-dgrid-bar {
  position: relative;
  z-index: 4;
  flex: none;
  display: flex;
  align-items: center;
  gap: 2px;
  box-sizing: border-box;
  height: var(--bar-h);
  padding: 0 4px 0 0;
  background: var(--bg-surface-sunken);
  border-bottom: 1px solid var(--border-default);
}

.xr-dgrid-bar .xr-dgrid-handle {
  flex: 1 1 auto;
  min-width: 0;
  box-sizing: border-box;
  height: 100%;
  padding: 0 var(--sp-2);
  gap: 6px;
  justify-content: flex-start;
  text-align: left;
  font-size: var(--fs-label);
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-secondary);
  background: transparent;
  border: 0;
  border-radius: 0;
  cursor: grab;
  touch-action: none; /* only the handle: the page keeps scrolling */
}

.xr-dgrid-bar .xr-dgrid-handle:hover {
  color: var(--text-primary);
  background: var(--bg-hover);
}

.xr-dgrid-bar .xr-dgrid-handle:active {
  cursor: grabbing;
}

.xr-dgrid-grip {
  width: 12px;
  height: 14px;
  flex: none;
  fill: currentColor;
  opacity: 0.7;
}

.xr-dgrid-title {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.xr-dgrid-bar .xr-dgrid-icon {
  flex: none;
  box-sizing: border-box;
  width: 28px;
  height: 28px;
  padding: 0;
  justify-content: center;
  color: var(--text-secondary);
  background: transparent;
  border: 0;
  border-radius: var(--radius-sm);
}

.xr-dgrid-bar .xr-dgrid-icon:hover {
  color: var(--text-primary);
  background: var(--bg-hover);
}

.xr-dgrid-bar .xr-dgrid-remove:hover {
  color: var(--negative);
}

.xr-dgrid-icon svg {
  width: 14px;
  height: 14px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* A quarter-width tile at the 8-column breakpoint (about 105 px): the grip and two icons, no title. */
@container dgitem (max-width: 170px) {
  .xr-dgrid-title {
    display: none;
  }
  .xr-dgrid-bar .xr-dgrid-handle {
    flex: 0 0 auto;
    width: 28px;
    padding: 0;
    justify-content: center;
  }
  .xr-dgrid-bar {
    justify-content: space-between;
    padding-left: 0;
  }
  .xr-dgrid-body > .xr-dashboard-card,
  .xr-dgrid-body > .xr-dw-card {
    padding: var(--sp-2) var(--sp-3);
  }
  .xr-dgrid-body .xr-dashboard-card-head,
  .xr-dgrid-body .xr-dw-head {
    margin-bottom: var(--sp-1);
  }
  .xr-dgrid-body .xr-dashboard-card-title,
  .xr-dgrid-body .xr-dw-title {
    font-size: 10px;
  }
}

.xr-dgrid-handle:focus-visible,
.xr-dgrid-btn:focus-visible,
.xr-dgrid-resize:focus-visible,
.xr-dpanel-card:focus-visible,
.xr-dpanel-tab:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

/* The resize corner sits INSIDE the frame, bottom right, with its own hit area. */
.xr-dgrid-resize {
  position: absolute;
  right: 0;
  bottom: 0;
  z-index: 4;
  box-sizing: border-box;
  width: 26px;
  height: 26px;
  padding: 0;
  color: var(--text-secondary);
  background: transparent;
  border: 0;
  border-radius: 0;
  cursor: nwse-resize;
  touch-action: none;
}

.xr-dgrid-resize::before {
  content: "";
  position: absolute;
  right: 5px;
  bottom: 5px;
  width: 11px;
  height: 11px;
  opacity: 0.75;
  background: linear-gradient(
    135deg,
    transparent 0 42%,
    currentColor 42% 52%,
    transparent 52% 68%,
    currentColor 68% 78%,
    transparent 78%
  );
}

.xr-dgrid-resize:hover {
  background: transparent;
  border: 0;
  color: var(--accent);
}

/* ---------- panel ---------- */

.xr-dpanel {
  position: sticky;
  top: var(--sp-4);
  box-sizing: border-box;
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  max-height: calc(100vh - 2 * var(--sp-4));
  overflow: auto;
  animation: xr-panel-in 0.2s ease-out both;
}

@keyframes xr-panel-in {
  from {
    opacity: 0;
    transform: translateX(8px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

/* Under the grid (tablet, phone) the panel is part of the page, not a floating column. */
@media (max-width: 1199px) {
  .xr-dpanel {
    position: static;
    max-height: none;
  }
}

.xr-dpanel-tabs {
  display: flex;
  border-bottom: 1px solid var(--border-default);
}

.xr-dpanel-tab {
  flex: 1;
  justify-content: center;
  height: 40px;
  font: inherit;
  font-weight: 500;
  padding: 0 var(--sp-3);
  background: transparent;
  color: var(--text-secondary);
  border: 0;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  cursor: pointer;
}

.xr-dpanel-tab[aria-selected="true"] {
  color: var(--text-primary);
  border-bottom-color: var(--accent);
  font-weight: 600;
}

.xr-dpanel-body {
  display: flex;
  flex-direction: column;
  gap: var(--sp-3);
  padding: var(--sp-4);
}

.xr-dpanel-search,
.xr-dpanel-field input,
.xr-dpanel-field select,
.xr-dpanel-field textarea {
  font: inherit;
  width: 100%;
  box-sizing: border-box;
  min-height: 32px;
  padding: var(--sp-2);
  color: var(--text-primary);
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
}

.xr-dpanel-search:focus-visible,
.xr-dpanel-field input:focus-visible,
.xr-dpanel-field select:focus-visible,
.xr-dpanel-field textarea:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 0;
}

.xr-dpanel-cat h3,
.xr-dpanel-heading {
  margin: 0 0 var(--sp-2);
  font-size: var(--fs-label);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-secondary);
}

.xr-dpanel-sub {
  margin: var(--sp-4) 0 var(--sp-1);
  font-size: var(--fs-label);
  font-weight: 700;
  color: var(--text-primary);
}

.xr-dpanel-hint {
  margin: 0 0 var(--sp-2);
  font-size: var(--fs-label);
  line-height: 1.4;
  color: var(--text-secondary);
}

.xr-dpanel-cards {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
}

.xr-dpanel-card {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  gap: 2px;
  width: 100%;
  height: auto;
  min-height: 52px;
  box-sizing: border-box;
  font: inherit;
  text-align: left;
  padding: var(--sp-2) var(--sp-3);
  color: var(--text-primary);
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: border-color 0.15s ease, background-color 0.15s ease;
}

.xr-dpanel-card strong {
  font-weight: 600;
}

.xr-dpanel-card:hover {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 4%, var(--bg-surface));
}

.xr-dpanel-card span {
  font-size: var(--fs-label);
  font-weight: 400;
  color: var(--text-secondary);
}

.xr-dpanel-field {
  display: flex;
  flex-direction: column;
  gap: var(--sp-1);
  min-width: 0;
}

.xr-dpanel-field > span {
  font-size: var(--fs-label);
  font-weight: 600;
  text-transform: uppercase;
  color: var(--text-secondary);
}

.xr-dpanel-row {
  display: flex;
  gap: var(--sp-2);
  flex-wrap: wrap;
}

.xr-dpanel-dates > .xr-dpanel-field {
  flex: 1 1 120px;
}

.xr-dpanel-row .xr-btn-ghost {
  height: 32px;
}

.xr-dpanel-remove {
  align-self: flex-start;
  height: 32px;
}

/* ---------- narrow screens: one column, no gestures ---------- */

@media (max-width: 767px) {
  .xr-dgrid {
    grid-auto-rows: auto;
  }
  /* One column: the stored row span would make a KPI tile 3 rows (216 px) tall; the card sizes itself. */
  .xr-dgrid-item {
    grid-row: auto !important;
    grid-column: 1 / -1 !important;
  }
  .xr-dgrid-edit {
    background-image: none;
    padding-bottom: 0;
  }
  .xr-dgrid-resize,
  .xr-dgrid-grip {
    display: none;
  }
  /* no move gesture here: the strip is just the title and its two buttons */
  .xr-dgrid-edit .xr-dgrid-body {
    min-height: 96px;
  }
  .xr-dtb-spacer {
    display: none;
  }
}

/* ---------- new widgets (container: the widget's own width) ---------- */

.xr-dwidget {
  container-type: inline-size;
  height: 100%;
  box-sizing: border-box;
  overflow: auto;
}

.xr-dwidget-kpi {
  display: flex;
  flex-direction: column;
  gap: var(--sp-1);
}

.xr-dwidget-kpi-value {
  font-size: 32px;
  /* shrinks with the tile: a quarter-width tile at the 8-column breakpoint was cut to "91,1..." */
  font-size: clamp(18px, 13cqi, 32px);
  font-weight: 700;
  line-height: 1.1;
  color: var(--text-primary);
}

.xr-dwidget-stale {
  font-size: var(--fs-label);
  color: var(--text-secondary);
}

.xr-dwidget-bars {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
}

.xr-dwidget-bar {
  display: grid;
  grid-template-columns: minmax(60px, 30%) 1fr auto;
  align-items: center;
  gap: var(--sp-2);
  font-size: var(--fs-body);
}

.xr-dwidget-bar-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.xr-dwidget-bar-track {
  height: 10px;
  background: var(--border-default);
  border-radius: 5px;
  overflow: hidden;
}

.xr-dwidget-bar-fill {
  display: block;
  height: 100%;
  background: var(--accent);
}

.xr-dwidget-bar-value {
  font-variant-numeric: tabular-nums;
}

.xr-dwidget-svg svg {
  display: block;
  width: 100%;
  height: 120px;
}

.xr-dwidget-svg rect {
  fill: var(--accent);
}

.xr-dwidget-svg polyline {
  stroke: var(--accent);
}

.xr-dwidget-legend {
  list-style: none;
  margin: var(--sp-2) 0 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-1) var(--sp-3);
  font-size: var(--fs-label);
  color: var(--text-secondary);
}

/* A narrow widget drops the legend (the aria-label keeps the summary). */
@container (max-width: 220px) {
  .xr-dwidget-legend {
    display: none;
  }
}

.xr-dwidget-note p {
  margin: 0 0 var(--sp-2);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}


/* ---------- touch and motion ---------- */

/* Fingers: bigger targets for the strip, its buttons, the resize corner and the toolbar (the gestures themselves are
   pointer events, so touch, pen and mouse share one code path; `touch-action: none` is on the handle and the corner). */
@media (pointer: coarse) {
  .xr-dgrid {
    --bar-h: 42px;
  }
  .xr-dgrid-bar .xr-dgrid-icon {
    width: 38px;
    height: 38px;
  }
  .xr-dgrid-resize {
    width: 38px;
    height: 38px;
  }
  .xr-dgrid-resize::before {
    right: 8px;
    bottom: 8px;
    width: 14px;
    height: 14px;
  }
  .xr-dash-toolbar .xr-dtb-btn {
    height: 40px;
  }
  .xr-dpanel-tab {
    height: 46px;
  }
  .xr-dpanel-card {
    min-height: 56px;
  }
  @container dgitem (max-width: 170px) {
    .xr-dgrid-bar .xr-dgrid-handle,
    .xr-dgrid-bar .xr-dgrid-icon {
      width: 34px;
    }
  }
}

@media (prefers-reduced-motion: reduce) {
  .xr-dgrid-item,
  .xr-dgrid-edit .xr-dgrid-item,
  .xr-dgrid-ghost,
  .xr-dpanel,
  .xr-dash-toolbar.is-editing,
  .xr-dpanel-card {
    animation: none;
    transition: none;
  }
}
