/* ═══════════════════════════════════════════════════════
   Dashboard Builder Layout
   ═══════════════════════════════════════════════════════ */

.builder-layout {
  display: grid;
  grid-template-columns: 260px 1fr 320px;
  grid-template-rows: var(--header-h) 1fr;
  height: 100vh;
  overflow: hidden;
}

/* ── Builder Topbar ─────────────────────────────────────── */
.builder-bar {
  grid-column: 1 / -1;
  height: var(--header-h);
  background: var(--bg-sidebar);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 10px;
  z-index: 100;
}
.builder-bar-logo {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
  text-decoration: none;
  margin-right: 8px;
  white-space: nowrap;
}
.builder-bar-logo .logo-icon {
  width: 28px; height: 28px;
  background: linear-gradient(135deg, var(--orange), #ff5722);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
}
.dash-name-input {
  background: var(--bg-input);
  border: 1px solid var(--border-l);
  border-radius: var(--radius);
  padding: 6px 12px;
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  font-family: inherit;
  outline: none;
  min-width: 200px;
}
.dash-name-input:focus { border-color: var(--orange); }
.bar-divider { width: 1px; height: 24px; background: var(--border-l); }
.bar-spacer  { flex: 1; }

/* ── Left Panel Library ─────────────────────────────────── */
.panel-library {
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.lib-section { padding: 12px; }
.lib-section-title {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-dim);
  padding: 8px 4px 6px;
}

.panel-type-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 9px 10px;
  border-radius: var(--radius);
  border: 1px solid transparent;
  background: none;
  color: var(--text-muted);
  font-size: 13px;
  font-family: inherit;
  cursor: grab;
  transition: all 0.15s;
  text-align: left;
}
.panel-type-btn:hover { background: var(--bg-card); color: var(--text); border-color: var(--border-l); }
.panel-type-btn:active { cursor: grabbing; background: var(--orange-dim); color: var(--orange); }
.panel-type-btn .pt-icon { font-size: 18px; width: 24px; text-align: center; }
.panel-type-btn .pt-name { font-weight: 500; }
.panel-type-btn .pt-desc { font-size: 11px; color: var(--text-dim); }

.lib-divider { height: 1px; background: var(--border); margin: 4px 0; }

.quick-panel-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 7px 10px;
  border-radius: var(--radius);
  border: none;
  background: none;
  color: var(--text-dim);
  font-size: 12px;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.15s;
  text-align: left;
}
.quick-panel-btn:hover { background: var(--bg-card); color: var(--text-muted); }
.quick-panel-btn .dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

/* ── Canvas Area ────────────────────────────────────────── */
.canvas-area {
  overflow: auto;
  background:
    radial-gradient(ellipse 60% 30% at 50% 0%, rgba(245,132,0,0.04), transparent),
    var(--bg);
  position: relative;
}

.canvas-toolbar {
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 10;
}

.time-range-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-card);
  border: 1px solid var(--border-l);
  border-radius: var(--radius);
  padding: 6px 12px;
  font-size: 13px;
  color: var(--text-muted);
  cursor: pointer;
}
.time-range-btn:hover { color: var(--text); border-color: var(--text-dim); }

.refresh-btn {
  background: var(--bg-card);
  border: 1px solid var(--border-l);
  border-radius: var(--radius);
  padding: 6px 10px;
  font-size: 13px;
  color: var(--text-muted);
  cursor: pointer;
}
.refresh-btn:hover { color: var(--text); }

.canvas {
  padding: 16px;
  display: grid;
  grid-template-columns: repeat(24, 1fr);
  gap: 4px;
  min-height: calc(100vh - var(--header-h) - 50px);
  align-content: start;
  position: relative;
}

.canvas.drag-over { background: rgba(245,132,0,0.03); }

/* ── Canvas Panel ───────────────────────────────────────── */
.canvas-panel {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
  transition: border-color 0.15s, box-shadow 0.15s;
  cursor: pointer;
  user-select: none;
}
.canvas-panel:hover { border-color: var(--border-l); }
.canvas-panel.selected {
  border-color: var(--orange);
  box-shadow: 0 0 0 1px var(--orange), inset 0 0 0 1px rgba(245,132,0,0.1);
}
.canvas-panel.dragging {
  opacity: 0.6;
  border-style: dashed;
  border-color: var(--orange);
}

.panel-header {
  padding: 8px 12px 6px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}
.panel-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.panel-menu-btn {
  opacity: 0;
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 16px;
  transition: all 0.15s;
  flex-shrink: 0;
}
.canvas-panel:hover .panel-menu-btn { opacity: 1; }
.panel-menu-btn:hover { background: var(--border); color: var(--text); }

.panel-body {
  flex: 1;
  padding: 4px 12px 8px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Resize handle */
.panel-resize-handle {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 16px;
  height: 16px;
  cursor: se-resize;
  opacity: 0;
  transition: opacity 0.15s;
}
.canvas-panel:hover .panel-resize-handle { opacity: 1; }
.panel-resize-handle::after {
  content: '';
  position: absolute;
  bottom: 3px;
  right: 3px;
  width: 10px;
  height: 10px;
  border-right: 2px solid var(--border-l);
  border-bottom: 2px solid var(--border-l);
  border-bottom-right-radius: 2px;
}

/* Drop ghost */
.drop-ghost {
  background: rgba(245,132,0,0.08);
  border: 2px dashed var(--orange);
  border-radius: var(--radius);
  pointer-events: none;
}

/* Empty canvas hint */
.canvas-empty {
  grid-column: 1 / -1;
  height: 400px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--text-dim);
  border: 2px dashed var(--border-l);
  border-radius: var(--radius-lg);
  margin: 20px 0;
  transition: all 0.2s;
}
.canvas-empty:hover { border-color: var(--border-l); background: var(--bg-card); }
.canvas-empty .ce-icon { font-size: 48px; opacity: 0.4; }
.canvas-empty h3 { font-size: 17px; color: var(--text-muted); }
.canvas-empty p { font-size: 13px; text-align: center; max-width: 300px; }

/* ── Right Properties Panel ─────────────────────────────── */
.properties-panel {
  background: var(--bg-sidebar);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}
.props-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.props-header h4 { font-size: 14px; font-weight: 600; }
.props-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 24px;
  text-align: center;
  color: var(--text-dim);
}
.props-empty .pe-icon { font-size: 36px; opacity: 0.3; margin-bottom: 6px; }
.props-empty p { font-size: 13px; }

.props-section { padding: 16px; border-bottom: 1px solid var(--border); }
.props-section-title {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  margin-bottom: 12px;
}

/* ── Panel Previews / Charts ────────────────────────────── */
.panel-viz-wrapper { width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; }

/* Stat panel */
.viz-stat {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  gap: 4px;
}
.viz-stat-value {
  font-size: clamp(24px, 4vw, 48px);
  font-weight: 700;
  line-height: 1;
}
.viz-stat-label {
  font-size: 12px;
  color: var(--text-muted);
}
.viz-stat-sparkline { width: 80%; opacity: 0.5; }

/* Gauge */
.viz-gauge { position: relative; display: flex; align-items: center; justify-content: center; width: 100%; height: 100%; }
.viz-gauge svg { max-height: 100%; }

/* Time series */
.viz-timeseries { width: 100%; height: 100%; }
.viz-timeseries svg { width: 100%; height: 100%; }

/* Bar chart */
.viz-barchart { width: 100%; height: 100%; display: flex; align-items: flex-end; gap: 4px; padding-bottom: 8px; }
.viz-bar {
  flex: 1;
  border-radius: 2px 2px 0 0;
  transition: opacity 0.2s;
  min-height: 4px;
}

/* Table */
.viz-table { width: 100%; font-size: 11px; border-collapse: collapse; }
.viz-table th { background: rgba(0,0,0,0.3); padding: 4px 8px; text-align: left; color: var(--text-dim); font-weight: 600; font-size: 10px; text-transform: uppercase; }
.viz-table td { padding: 4px 8px; border-bottom: 1px solid var(--border); color: var(--text-muted); }
.viz-table tr:last-child td { border-bottom: none; }

/* Heatmap */
.viz-heatmap { display: grid; gap: 2px; width: 100%; height: 100%; }
.viz-heatmap-cell { border-radius: 1px; }

/* ── Panel Context Menu ─────────────────────────────────── */
.context-menu {
  position: fixed;
  background: var(--bg-modal);
  border: 1px solid var(--border-l);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 500;
  min-width: 160px;
  overflow: hidden;
  animation: fadeIn 0.1s ease;
}
.ctx-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 14px;
  font-size: 13px;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.1s;
}
.ctx-item:hover { background: var(--bg-card); color: var(--text); }
.ctx-item.danger { color: var(--red); }
.ctx-item.danger:hover { background: var(--red-dim); }
.ctx-divider { height: 1px; background: var(--border); margin: 4px 0; }

/* ── Dashboard Variables Bar ────────────────────────────── */
.variables-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  flex-shrink: 0;
}
.var-item {
  display: flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}
.var-label { font-size: 12px; color: var(--text-dim); }
.var-select {
  background: var(--bg-card);
  border: 1px solid var(--border-l);
  border-radius: var(--radius);
  padding: 4px 8px;
  font-size: 12px;
  color: var(--text);
  font-family: inherit;
  outline: none;
  cursor: pointer;
}

/* ── Row Group Header ───────────────────────────────────── */
.row-header {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 4px;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 600;
  border-radius: var(--radius);
  transition: background 0.15s;
}
.row-header:hover { background: var(--bg-card); }
.row-header-line { flex: 1; height: 1px; background: var(--border-l); }

/* ── Scrollable sidebar ─────────────────────────────────── */
.panel-library::-webkit-scrollbar-track { background: var(--bg-sidebar); }
.properties-panel::-webkit-scrollbar-track { background: var(--bg-sidebar); }

/* ── Status Bar ─────────────────────────────────────────── */
.status-bar {
  grid-column: 1 / -1;
  height: 26px;
  background: var(--bg-sidebar);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 14px;
  gap: 16px;
  font-size: 11px;
  color: var(--text-dim);
  position: sticky;
  bottom: 0;
  z-index: 10;
}
.status-bar span { display: flex; align-items: center; gap: 4px; }
.status-bar .status-dot { width: 6px; height: 6px; border-radius: 50%; }

/* ── Builder page body ──────────────────────────────────── */
.builder-page {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

/* ── Builder Header ─────────────────────────────────────── */
.builder-header {
  height: var(--header-h);
  background: var(--bg-sidebar);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 12px;
  gap: 10px;
  flex-shrink: 0;
  z-index: 100;
}
.header-spacer { flex: 1; }
.header-toolbar {
  display: flex;
  align-items: center;
  gap: 6px;
}
.toolbar-divider { width: 1px; height: 20px; background: var(--border-l); margin: 0 2px; }

/* Override layout — no header row since header is outside */
.builder-layout {
  flex: 1;
  overflow: hidden;
  display: grid;
  grid-template-columns: 260px 1fr 320px;
  grid-template-rows: 1fr;
}

/* ── Connection status dot ──────────────────────────────── */
.conn-dot {
  display: inline-block;
  width: 7px; height: 7px;
  border-radius: 50%;
  flex-shrink: 0;
}
.conn-dot.online  { background: var(--green); box-shadow: 0 0 4px var(--green); }
.conn-dot.offline { background: var(--text-dim); }

/* ── Panel type list wrappers ───────────────────────────── */
.panel-type-list { display: flex; flex-direction: column; gap: 2px; }
.quick-panel-list { display: flex; flex-direction: column; gap: 1px; }
.lib-templates-btn { display: block; text-align: center; }

/* Panel type button icon/name aliases */
.ptb-icon { font-size: 16px; width: 22px; text-align: center; }
.ptb-name { font-weight: 500; font-size: 12px; }

/* ── Canvas toolbar controls ────────────────────────────── */
.ct-info { font-size: 12px; color: var(--text-dim); flex: 1; }
.ct-controls { display: flex; align-items: center; gap: 8px; }
.ct-refresh, .ct-timerange {
  background: var(--bg-card);
  border: 1px solid var(--border-l);
  border-radius: var(--radius);
  padding: 5px 10px;
  font-size: 12px;
  color: var(--text);
  font-family: inherit;
  outline: none;
  cursor: pointer;
}
.ct-refresh:hover, .ct-timerange:hover { border-color: var(--orange); }

/* ── Status bar extras ──────────────────────────────────── */
.sb-divider { color: var(--border-l); }
.sb-spacer  { flex: 1; }

/* ── Connection how-to ──────────────────────────────────── */
.conn-how {
  margin-top: 16px;
  background: var(--bg-card);
  border: 1px solid var(--border-l);
  border-radius: var(--radius);
  padding: 12px 14px;
}
.conn-how-title {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  margin-bottom: 8px;
}
.conn-how-steps {
  margin: 0;
  padding-left: 18px;
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.8;
}
.conn-how-steps li { margin: 0; }

/* ── form-row-2 / form-row-3 ────────────────────────────── */
.form-row-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.form-row-3 {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 10px;
}

/* ── Panel editor modal wide ────────────────────────────── */
.modal-wide { max-width: 700px; width: 95vw; }

/* ── Panel type grid in editor ──────────────────────────── */
.panel-type-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}
.panel-type-option {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 10px 6px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg-card);
  cursor: pointer;
  transition: all 0.15s;
}
.panel-type-option:hover { border-color: var(--border-l); background: var(--bg-input); }
.panel-type-option.active { border-color: var(--orange); background: var(--orange-dim); }
.pto-icon { font-size: 20px; }
.pto-name { font-size: 10px; color: var(--text-dim); text-align: center; }

/* ── Threshold row ──────────────────────────────────────── */
.threshold-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}
.threshold-color-dot {
  width: 14px; height: 14px;
  border-radius: 50%;
  flex-shrink: 0;
  border: 2px solid rgba(255,255,255,0.15);
}

/* ── Query row ──────────────────────────────────────────── */
.query-row textarea.form-control {
  resize: vertical;
  font-family: 'Fira Code', 'Cascadia Code', monospace;
  font-size: 12px;
}

/* ── Responsive ─────────────────────────────────────────── */
@media (max-width: 1200px) {
  .builder-layout { grid-template-columns: 220px 1fr 280px; }
}
@media (max-width: 900px) {
  .builder-layout { grid-template-columns: 1fr; }
  .panel-library, .properties-panel { display: none; }
}
