/*
 * Reporting Engine — three-column drag/drop builder.
 * Uses the design tokens from style.css.
 */

#view-report-engine .topbar { gap: .5rem; }

.re-shell {
  display: grid;
  grid-template-columns: 280px 1fr;
  gap: 1rem;
  padding: 16px 20px 20px;
  align-items: stretch;
  flex: 1;
  min-height: 0;
}

/* ---------- Palette (left) ---------- */

.re-palette {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
}
.re-palette-head {
  padding: .75rem .75rem .5rem;
  display: flex;
  flex-direction: column;
  gap: .4rem;
  border-bottom: 1px solid var(--divider);
  background: var(--surface-2);
}
.re-label {
  font-size: .72rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-muted);
  font-weight: 600;
}
.re-input {
  width: 100%;
  padding: .4rem .55rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: .85rem;
  box-sizing: border-box;
}
.re-input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px var(--primary-soft);
}
textarea.re-input { font-family: ui-monospace, Menlo, Consolas, monospace; }
.re-link {
  background: none;
  border: 0;
  color: var(--primary);
  font-size: .8rem;
  text-align: left;
  padding: .25rem 0;
  cursor: pointer;
}
.re-link:hover { text-decoration: underline; }

.re-field-list {
  flex: 1;
  overflow-y: auto;
  padding: .5rem .5rem 1rem;
}
.re-field-group { margin-bottom: .85rem; }
.re-field-group-head {
  font-size: .68rem;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-faint);
  padding: .25rem .25rem .25rem;
  display: flex; justify-content: space-between;
}
.re-field-group-count {
  background: var(--surface-3);
  padding: 0 .4rem;
  border-radius: 9px;
}
.re-field {
  display: flex;
  align-items: center;
  gap: .4rem;
  padding: .35rem .45rem;
  margin: 2px 0;
  background: var(--surface-2);
  border: 1px solid var(--divider);
  border-radius: var(--radius-sm);
  cursor: grab;
  font-size: .82rem;
  user-select: none;
}
.re-field:hover { border-color: var(--primary-soft); background: var(--surface); }
.re-field.dragging { opacity: .4; }
.re-field-name {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.re-field-samples {
  font-size: .7rem;
  color: var(--text-faint);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100px;
}
.re-field-computed {
  background: var(--primary-soft);
  color: var(--primary-hover);
  font-style: italic;
  border-radius: 3px;
  padding: 0 4px;
  font-size: .7rem;
  font-weight: 700;
}

.re-type {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border-radius: 4px;
  font-size: .7rem;
  font-weight: 700;
  font-family: ui-monospace, monospace;
  flex-shrink: 0;
}
.re-type-number { background: #cddcd8; color: #016870; }
.re-type-date { background: #efd9c7; color: #964118; }
.re-type-text { background: #e7e4dc; color: #6b6961; }
.re-type-boolean { background: #dbe8ce; color: #437a22; }
[data-theme="dark"] .re-type-number { background: #1f3a3d; color: #6cb1bb; }
[data-theme="dark"] .re-type-date { background: #3a2a1d; color: #efd9c7; }
[data-theme="dark"] .re-type-text { background: #252422; color: #adaba6; }
[data-theme="dark"] .re-type-boolean { background: #1f3322; color: #8fc36b; }

/* ---------- Canvas (center) ---------- */

.re-canvas {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
}

.re-meta-bar {
  padding: .65rem .75rem;
  display: flex;
  gap: .5rem;
  align-items: center;
  border-bottom: 1px solid var(--divider);
  background: var(--surface-2);
  flex-wrap: wrap;
}
.re-name { max-width: 280px; font-weight: 600; }
.re-desc { flex: 1; min-width: 200px; }
.re-meta-stats {
  font-size: .75rem;
  color: var(--text-muted);
  margin-left: auto;
}

.re-dropzones {
  padding: .75rem;
  display: flex;
  flex-direction: column;
  gap: .6rem;
}
.re-dz-row {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: .6rem;
}

.re-dropzone {
  border: 1.5px dashed var(--border);
  border-radius: var(--radius-md);
  background: var(--surface-2);
  min-height: 76px;
  display: flex;
  flex-direction: column;
  transition: background .12s, border-color .12s;
}
.re-dropzone.drag-over {
  background: var(--primary-soft);
  border-color: var(--primary);
  border-style: solid;
}
.re-dz-head {
  padding: .35rem .55rem .25rem;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: .5rem;
}
.re-dz-title {
  font-size: .72rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  font-weight: 700;
  color: var(--text);
}
.re-dz-hint {
  font-size: .7rem;
  color: var(--text-faint);
}
.re-dz-body {
  padding: .35rem .55rem .55rem;
  display: flex;
  flex-wrap: wrap;
  gap: .35rem;
  flex: 1;
  align-content: flex-start;
}
.re-dz-placeholder {
  font-size: .78rem;
  color: var(--text-faint);
  font-style: italic;
  padding: .25rem 0;
}

.re-chip {
  display: inline-flex;
  align-items: center;
  gap: .35rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: .2rem .55rem .2rem .35rem;
  font-size: .8rem;
  cursor: grab;
  user-select: none;
  max-width: 100%;
}
.re-chip:hover { border-color: var(--primary); }
.re-chip.dragging { opacity: .4; }
.re-chip-name {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 160px;
}
.re-chip-detail {
  font-size: .7rem;
  color: var(--primary);
  background: var(--primary-soft);
  padding: 0 .35rem;
  border-radius: 4px;
  text-transform: lowercase;
}
.re-chip-remove {
  background: transparent;
  border: 0;
  color: var(--text-faint);
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  padding: 0 .15rem;
  border-radius: 50%;
}
.re-chip-remove:hover { color: var(--error); background: var(--error-soft); }

/* ---------- Output controls + result ---------- */

.re-output-bar {
  padding: .55rem .75rem;
  display: flex;
  gap: .65rem;
  align-items: center;
  border-top: 1px solid var(--divider);
  background: var(--surface-2);
  flex-wrap: wrap;
}
.re-output-modes {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--surface);
}
.re-mode-btn {
  background: transparent;
  border: 0;
  border-right: 1px solid var(--divider);
  padding: .35rem .65rem;
  font-size: .8rem;
  cursor: pointer;
  color: var(--text);
}
.re-mode-btn:last-child { border-right: 0; }
.re-mode-btn.active { background: var(--primary); color: white; }
.re-toggle {
  display: inline-flex;
  align-items: center;
  gap: .35rem;
  font-size: .8rem;
  color: var(--text-muted);
}

.re-result {
  flex: 1;
  overflow: auto;
  padding: .75rem;
  background: var(--surface);
  position: relative;
  min-height: 280px;
}
.re-result.re-loading::after {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(255,255,255,.6);
  background: color-mix(in srgb, var(--surface) 60%, transparent);
}
[data-theme="dark"] .re-result.re-loading::after {
  background: rgba(0,0,0,.5);
}

.re-empty, .re-empty-mini {
  text-align: center;
  color: var(--text-faint);
  padding: 2rem 1rem;
  font-style: italic;
}
.re-empty-mini { padding: 1rem .5rem; font-size: .82rem; }

.re-error {
  padding: 1rem;
  background: var(--error-soft);
  color: var(--error);
  border-radius: var(--radius-sm);
  white-space: pre-wrap;
}
.re-warnings {
  padding: 0 .75rem .5rem;
}
.re-warning {
  background: var(--warning-soft);
  color: var(--warning);
  padding: .35rem .55rem;
  border-radius: var(--radius-sm);
  font-size: .78rem;
  margin-bottom: .25rem;
}

/* ---------- Tables / pivot ---------- */

.re-table-wrap {
  overflow: auto;
  border: 1px solid var(--divider);
  border-radius: var(--radius-sm);
  background: var(--surface);
  max-width: 100%;
}
.re-table {
  border-collapse: collapse;
  width: 100%;
  font-size: .82rem;
}
.re-table th, .re-table td {
  border: 1px solid var(--divider);
  padding: .35rem .55rem;
  text-align: left;
  vertical-align: top;
}
.re-table thead th {
  background: var(--surface-2);
  font-weight: 600;
  position: sticky;
  top: 0;
  z-index: 1;
}
.re-table tbody tr:nth-child(even) td { background: var(--surface-2); }
.re-cell-measure { text-align: right; font-variant-numeric: tabular-nums; }
.re-cell-dim { font-weight: 500; }
.re-cell-total { background: var(--surface-3) !important; font-weight: 600; }
.re-cell-grand { background: var(--primary-soft) !important; color: var(--primary-hover); font-weight: 700; }

.re-pivot .re-pivot-corner { background: var(--surface-3); }
.re-pivot .re-pivot-col-head {
  text-align: center;
  background: var(--surface-2);
}
.re-pivot .re-pivot-row-head {
  background: var(--surface-2);
  font-weight: 600;
  text-align: left;
}
.re-pivot .re-pivot-measure {
  background: var(--surface-3);
  font-size: .72rem;
  text-transform: uppercase;
  letter-spacing: .03em;
  text-align: right;
}
.re-pivot .re-pivot-total-col {
  background: var(--surface-3);
  text-align: center;
}
.re-pivot-totals-row { background: var(--surface-3); font-weight: 600; }

/* ---------- Cards / chart ---------- */

.re-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: .75rem;
}
.re-card {
  background: var(--surface-2);
  border: 1px solid var(--divider);
  border-radius: var(--radius-md);
  padding: .85rem 1rem;
}
.re-card-label {
  font-size: .72rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-muted);
  margin-bottom: .35rem;
}
.re-card-value {
  font-size: 1.45rem;
  font-weight: 700;
  color: var(--primary);
  font-variant-numeric: tabular-nums;
}

.re-chart-canvas {
  width: 100% !important;
  max-height: 480px;
}

/* ---------- Popover ---------- */

.re-popover {
  position: fixed;
  z-index: 50;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  padding: .65rem;
  min-width: 240px;
  max-width: 320px;
}
.re-popover[hidden] { display: none; }
.re-pop {
  display: flex;
  flex-direction: column;
  gap: .35rem;
}
.re-pop-head {
  display: flex;
  align-items: center;
  gap: .35rem;
  padding-bottom: .35rem;
  border-bottom: 1px solid var(--divider);
  margin-bottom: .25rem;
}
.re-pop-head strong { flex: 1; font-size: .85rem; }
.re-pop-close {
  background: transparent;
  border: 0;
  font-size: 1.05rem;
  color: var(--text-faint);
  cursor: pointer;
  line-height: 1;
}
.re-pop-label {
  font-size: .7rem;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-top: .35rem;
  letter-spacing: .04em;
  font-weight: 600;
}
.re-pop select, .re-pop input, .re-pop textarea {
  width: 100%;
  padding: .35rem .5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: .82rem;
  box-sizing: border-box;
}
.re-pop textarea { resize: vertical; }

/* ---------- Templates list ---------- */

.modal-lg { width: min(720px, 92vw); }
.modal-md { width: min(560px, 92vw); }

.re-templates-list {
  display: flex;
  flex-direction: column;
  gap: .5rem;
  max-height: 60vh;
  overflow-y: auto;
}
.re-template {
  display: flex;
  gap: 1rem;
  padding: .65rem .85rem;
  border: 1px solid var(--divider);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}
.re-template:hover { border-color: var(--primary-soft); }
.re-template-main { flex: 1; min-width: 0; }
.re-template-name {
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: .4rem;
}
.re-curated-badge {
  font-size: .65rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .04em;
  background: var(--primary-soft);
  color: var(--primary-hover);
  padding: 1px 6px;
  border-radius: 4px;
}
.re-template-desc {
  font-size: .8rem;
  color: var(--text-muted);
  margin-top: .15rem;
}
.re-template-meta {
  font-size: .7rem;
  color: var(--text-faint);
  margin-top: .35rem;
}
.re-template-actions {
  display: flex;
  gap: .35rem;
  flex-shrink: 0;
  align-items: flex-start;
}
.re-template-actions .btn { padding: .3rem .65rem; font-size: .78rem; }

.re-computed-error {
  color: var(--error);
  font-size: .82rem;
  margin-top: .4rem;
  white-space: pre-wrap;
}

/* ---------- Generate button (prominent) ---------- */

.re-generate-btn {
  display: inline-flex;
  align-items: center;
  gap: .45rem;
  padding: .5rem 1.1rem;
  background: linear-gradient(135deg, var(--primary), var(--primary-hover));
  color: white;
  border: 0;
  border-radius: var(--radius-md);
  font-weight: 600;
  font-size: .9rem;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(1, 104, 112, 0.3);
  transition: transform .12s, box-shadow .12s;
}
.re-generate-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 14px rgba(1, 104, 112, 0.4);
}
.re-generate-btn:active { transform: translateY(0); }
.re-generate-btn svg { width: 16px; height: 16px; }

/* ---------- Fullscreen output modal ---------- */

.re-fullscreen {
  position: fixed;
  inset: 0;
  z-index: 1500;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  animation: re-fs-fade-in .18s ease-out;
}
.re-fullscreen.hidden { display: none; }

@keyframes re-fs-fade-in {
  from { opacity: 0; transform: scale(.99); }
  to { opacity: 1; transform: scale(1); }
}

body.re-fs-open { overflow: hidden; }

.re-fs-header {
  background: var(--surface);
  border-bottom: 2px solid var(--primary);
  padding: 14px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}
.re-fs-title-block { min-width: 220px; }
.re-fs-title-block h2 {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--primary);
  margin: 0;
}
.re-fs-meta {
  font-size: .78rem;
  color: var(--text-muted);
  margin-top: 3px;
}

.re-fs-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.re-fs-mode-tabs {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--surface-2);
}

.re-fs-progress {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}
.re-fs-progress-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 36px 48px;
  box-shadow: var(--shadow-md);
  text-align: center;
  min-width: 360px;
}
.re-fs-spinner {
  width: 44px;
  height: 44px;
  border: 4px solid var(--surface-3);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: re-spin 0.8s linear infinite;
  margin: 0 auto 14px;
}
@keyframes re-spin { to { transform: rotate(360deg); } }
.re-fs-progress-title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 14px;
}
.re-fs-progress-bar {
  width: 100%;
  height: 8px;
  background: var(--surface-3);
  border-radius: 4px;
  overflow: hidden;
  margin-bottom: 10px;
}
.re-fs-progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), var(--primary-hover));
  border-radius: 4px;
  width: 0%;
  transition: width 0.4s ease-out;
}
.re-fs-progress-step {
  font-size: .82rem;
  color: var(--text-muted);
}

.re-fs-body {
  flex: 1;
  overflow: auto;
  padding: 18px 24px 28px;
}
.re-fs-stats {
  font-size: .82rem;
  color: var(--text-muted);
  margin-bottom: 10px;
  padding: 8px 14px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  display: inline-block;
}
.re-fs-warnings { margin-bottom: 12px; }
.re-fs-result-area {
  background: var(--surface);
  border: 1px solid var(--divider);
  border-radius: var(--radius-md);
  overflow: auto;
  padding: 8px;
}
.re-fs-result-area .re-table-wrap { border: 0; }
.re-fs-result-area .re-table { font-size: .88rem; }
.re-fs-chart-canvas {
  width: 100% !important;
  max-height: calc(100vh - 240px);
}

.re-fs-error {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  font-size: .95rem;
  color: var(--error);
  background: var(--error-soft);
  margin: 24px;
  border-radius: var(--radius-md);
  white-space: pre-wrap;
}

/* ---------- Responsive ---------- */

@media (max-width: 1100px) {
  .re-shell { grid-template-columns: 1fr; }
  .re-dz-row { grid-template-columns: 1fr; }
}
