/* ===================================================================
   components.css — buttons, badges, modals, selects, data grid
   =================================================================== */

/* ── Buttons ────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 14px;
  font-family: var(--font);
  font-size: var(--font-size-sm);
  font-weight: 500;
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
  transition: background .13s, color .13s, border-color .13s, box-shadow .13s;
  outline: none;
  line-height: 1.4;
}

.btn:focus-visible {
  box-shadow: 0 0 0 3px rgba(37, 99, 235, .3);
}

.btn:disabled {
  opacity: .45;
  cursor: not-allowed;
  pointer-events: none;
}

/* Sizes */
.btn-sm  { padding: 4px 10px; font-size: var(--font-size-sm); }
.btn-xs  { padding: 3px 8px;  font-size: var(--font-size-xs); }
.btn-run { width: 100%; justify-content: center; padding: 8px; }

/* Variants */
.btn-primary {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.btn-primary:hover { background: var(--primary-dark); border-color: var(--primary-dark); }

.btn-outline {
  background: transparent;
  color: var(--text);
  border-color: var(--border-dark);
}
.btn-outline:hover { background: var(--bg); border-color: var(--secondary); }

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border-color: transparent;
}
.btn-ghost:hover { background: var(--bg); color: var(--text); }

.btn-danger {
  background: var(--danger);
  color: #fff;
  border-color: var(--danger);
}
.btn-danger:hover { background: #b91c1c; }

.btn-danger-hover:hover {
  color: var(--danger);
  border-color: var(--danger);
  background: var(--danger-light);
}

/* Icon-only button */
.btn-icon {
  padding: 4px 6px;
  min-width: unset;
}

/* ── Badge ──────────────────────────────────────────────────────── */
.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 100px;
  font-size: 11px;
  font-weight: 700;
  background: var(--border);
  color: var(--text-muted);
}

.badge-primary { background: var(--primary-light); color: var(--primary); }

/* ── Select ─────────────────────────────────────────────────────── */
.select {
  padding: 5px 28px 5px var(--sp-sm);
  font-family: var(--font);
  font-size: var(--font-size-sm);
  color: var(--text);
  background: var(--bg-panel) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E") no-repeat right 8px center;
  border: 1px solid var(--border-dark);
  border-radius: var(--radius-sm);
  appearance: none;
  cursor: pointer;
  outline: none;
  width: 100%;
}

.select:focus { border-color: var(--primary); }

/* ── Modal ──────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  animation: overlayIn .15s ease;
}

.modal-overlay[hidden] { display: none; }

@keyframes overlayIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.modal {
  background: var(--bg-panel);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  max-height: 90vh;
  width: 540px;
  animation: modalIn .15s ease;
}

.modal-lg { width: 760px; }

@keyframes modalIn {
  from { transform: translateY(-12px) scale(.97); opacity: 0; }
  to   { transform: translateY(0)     scale(1);   opacity: 1; }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--sp-md) var(--sp-lg);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.modal-header h3 {
  font-size: var(--font-size-lg);
  font-weight: 700;
  color: var(--text);
}

.modal-close {
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 18px;
  color: var(--text-muted);
  transition: background .1s;
}

.modal-close:hover { background: var(--bg); color: var(--text); }

.modal-body {
  flex: 1;
  overflow-y: auto;
  padding: var(--sp-lg);
}

.modal-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: var(--sp-sm);
  padding: var(--sp-md) var(--sp-lg);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

/* ── Dropdown ───────────────────────────────────────────────────── */
.dropdown-wrap {
  position: relative;
}

.dropdown {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  min-width: 200px;
  z-index: 500;
  animation: dropdownIn .1s ease;
}

.dropdown[hidden] { display: none; }

@keyframes dropdownIn {
  from { transform: translateY(-4px); opacity: 0; }
  to   { transform: translateY(0);    opacity: 1; }
}

.dropdown-list { padding: var(--sp-xs) 0; }

.dropdown-item {
  display: flex;
  align-items: center;
  gap: var(--sp-sm);
  padding: 7px var(--sp-md);
  font-size: var(--font-size-sm);
  color: var(--text);
  cursor: pointer;
  transition: background .1s;
}

.dropdown-item:hover { background: var(--bg); }

.dropdown-item-meta {
  font-size: var(--font-size-xs);
  color: var(--text-muted);
  margin-left: auto;
}

.dropdown-empty {
  padding: var(--sp-md);
  text-align: center;
  font-size: var(--font-size-sm);
  color: var(--text-light);
}

.dropdown-divider {
  height: 1px;
  background: var(--border);
  margin: var(--sp-xs) 0;
}

/* ── Context Menu ───────────────────────────────────────────────── */
.context-menu {
  position: fixed;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  padding: var(--sp-xs) 0;
  z-index: 2000;
  min-width: 160px;
  animation: dropdownIn .1s ease;
}

.context-menu[hidden] { display: none; }

.context-item {
  display: flex;
  align-items: center;
  gap: var(--sp-sm);
  padding: 6px var(--sp-md);
  font-size: var(--font-size-sm);
  color: var(--text);
  cursor: pointer;
  transition: background .1s;
}

.context-item:hover { background: var(--primary-light); color: var(--primary); }

/* ── Import Drop Zone ───────────────────────────────────────────── */
.drop-zone {
  border: 2px dashed var(--border-dark);
  border-radius: var(--radius-lg);
  padding: var(--sp-xl) var(--sp-lg);
  text-align: center;
  cursor: pointer;
  transition: border-color .15s, background .15s;
  background: var(--bg);
}

.drop-zone:hover,
.drop-zone:focus,
.drop-zone.drag-over {
  border-color: var(--primary);
  background: var(--primary-light);
  outline: none;
}

.drop-zone-icon {
  font-size: 36px;
  margin-bottom: var(--sp-sm);
  opacity: .6;
}

.drop-zone-main {
  font-size: var(--font-size-base);
  font-weight: 600;
  color: var(--text);
}

.drop-zone-sub {
  font-size: var(--font-size-sm);
  color: var(--text-muted);
  margin-top: 4px;
}

.import-options-row {
  margin-top: var(--sp-md);
  display: flex;
  align-items: center;
  gap: var(--sp-md);
}

/* Import preview */
.import-preview-header {
  display: flex;
  align-items: center;
  gap: var(--sp-md);
  margin-bottom: var(--sp-sm);
}

.import-file-name {
  font-size: var(--font-size-sm);
  font-weight: 600;
  color: var(--text);
}

.import-dim {
  font-size: var(--font-size-xs);
  color: var(--text-muted);
  background: var(--bg);
  padding: 2px 8px;
  border-radius: 100px;
  border: 1px solid var(--border);
}

/* Variable type chips above preview table */
.var-type-chips {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: var(--sp-sm);
}

.type-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 8px;
  border-radius: 100px;
  font-size: var(--font-size-xs);
  font-weight: 600;
  cursor: pointer;
  border: 1px solid transparent;
  transition: opacity .12s, box-shadow .12s;
  user-select: none;
}

.type-chip:hover { opacity: .85; box-shadow: 0 0 0 2px rgba(0,0,0,.15); }

.type-chip.type-numeric     { background: #dbeafe; color: var(--c-numeric);     border-color: #bfdbfe; }
.type-chip.type-categorical { background: #ede9fe; color: var(--c-categorical); border-color: #ddd6fe; }
.type-chip.type-date        { background: #cffafe; color: var(--c-date);        border-color: #a5f3fc; }
.type-chip.type-string      { background: #f1f5f9; color: var(--c-string);      border-color: var(--border-dark); }

/* Type chip dropdown (shown on click) */
.type-chip-picker {
  position: absolute;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  padding: var(--sp-xs) 0;
  z-index: 2000;
  min-width: 140px;
}

.type-chip-picker[hidden] { display: none; }

.type-chip-option {
  display: flex;
  align-items: center;
  gap: var(--sp-sm);
  padding: 6px var(--sp-md);
  font-size: var(--font-size-sm);
  cursor: pointer;
  transition: background .1s;
}

.type-chip-option:hover { background: var(--bg); }

/* Preview table */
.preview-table-wrap {
  overflow: auto;
  max-height: 260px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}

.preview-table {
  border-collapse: collapse;
  font-size: var(--font-size-xs);
  width: 100%;
  white-space: nowrap;
}

.preview-table th {
  position: sticky;
  top: 0;
  background: var(--bg-sidebar);
  padding: 5px var(--sp-sm);
  border-bottom: 1px solid var(--border);
  text-align: left;
  font-weight: 600;
  color: var(--text-muted);
}

.preview-table td {
  padding: 4px var(--sp-sm);
  border-bottom: 1px solid var(--border);
  color: var(--text);
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.preview-table tr:last-child td { border-bottom: none; }
.preview-table tr:hover td { background: var(--bg); }

.preview-note {
  font-size: var(--font-size-xs);
  color: var(--text-muted);
  margin-top: var(--sp-sm);
  font-style: italic;
}

/* ── Data Grid (Virtual Scroll) ─────────────────────────────────── */
.vg-table-wrap {
  position: relative;
  overflow: auto;
  height: 100%;
  width: 100%;
  background: var(--bg-panel);
}

/* Sticky corner + header */
.vg-thead {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--bg-sidebar);
}

.vg-header-row {
  display: flex;
  align-items: stretch;
}

.vg-cell {
  display: flex;
  align-items: center;
  padding: 0 6px;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  min-height: 28px;
  font-size: var(--font-size-xs);
  flex-shrink: 0;
  overflow: hidden;
  position: relative;
}

/* Row number cell */
.vg-rownum {
  width: 48px;
  min-width: 48px;
  justify-content: center;
  background: var(--bg-sidebar);
  color: var(--text-muted);
  font-size: 11px;
  cursor: default;
  user-select: none;
  flex-shrink: 0;
  position: sticky;
  left: 0;
  z-index: 10;
  border-right: 2px solid var(--border-dark);
}

.vg-rownum.selected-row {
  background: var(--primary-light);
  color: var(--primary);
  font-weight: 700;
}

/* Column header cell */
.vg-col-header {
  background: var(--bg-sidebar);
  font-weight: 600;
  cursor: pointer;
  user-select: none;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  gap: 1px;
  padding: 2px 6px;
  position: sticky;
  top: 0;
}

.vg-col-header:hover { background: #e2e8f0; }

.vg-col-header-top {
  display: flex;
  align-items: center;
  gap: 4px;
  width: 100%;
}

.vg-col-name {
  font-size: var(--font-size-xs);
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: calc(100% - 24px);
}

.vg-col-type {
  font-size: 9px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .3px;
}

/* Corner (row-num header) */
.vg-corner {
  width: 48px;
  min-width: 48px;
  flex-shrink: 0;
  background: var(--bg-sidebar);
  border-right: 2px solid var(--border-dark);
  border-bottom: 1px solid var(--border);
  position: sticky;
  left: 0;
  z-index: 30;
}

/* Data rows */
.vg-body {
  position: relative; /* virtual scroll spacer parent */
}

.vg-row {
  display: flex;
  align-items: stretch;
}

.vg-row:hover .vg-data-cell { background: #f8faff; }
.vg-row.selected .vg-data-cell { background: #eff6ff; }

.vg-data-cell {
  background: var(--bg-panel);
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: default;
}

.vg-data-cell.type-numeric     { color: #1e40af; }
.vg-data-cell.type-categorical { color: #5b21b6; }
.vg-data-cell.type-date        { color: #0e7490; }
.vg-data-cell.missing-val      { color: var(--text-light); font-style: italic; }

/* Cell in edit mode */
.vg-data-cell.editing {
  padding: 0;
  overflow: visible;
  z-index: 5;
}

.vg-edit-input {
  width: 100%;
  height: 100%;
  min-height: 28px;
  padding: 0 6px;
  border: 2px solid var(--primary);
  border-radius: 0;
  background: #fff;
  font-family: var(--font);
  font-size: var(--font-size-xs);
  color: var(--text);
  outline: none;
  box-shadow: 0 0 0 3px rgba(37,99,235,.15);
  position: relative;
  z-index: 10;
}

/* ── Output Tables (analysis results) ──────────────────────────── */
.result-section {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  margin-bottom: var(--sp-lg);
  overflow: hidden;
}

.result-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--sp-sm) var(--sp-md);
  border-bottom: 1px solid var(--border);
  background: var(--bg-sidebar);
}

.result-section-title {
  font-size: var(--font-size-sm);
  font-weight: 700;
  color: var(--text);
}

.result-section-actions {
  display: flex;
  gap: var(--sp-xs);
}

.result-table-wrap {
  overflow-x: auto;
}

/* APA-style result table */
.result-table {
  border-collapse: collapse;
  font-size: var(--font-size-sm);
  width: 100%;
}

.result-table thead tr {
  border-bottom: 2px solid var(--text);
}

.result-table th {
  padding: 6px var(--sp-md);
  text-align: right;
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
}

.result-table th:first-child { text-align: left; }

.result-table td {
  padding: 5px var(--sp-md);
  text-align: right;
  color: var(--text);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.result-table td:first-child { text-align: left; }

.result-table tfoot tr {
  border-top: 1px solid var(--text);
}

.result-table tfoot td {
  font-size: var(--font-size-xs);
  color: var(--text-muted);
  border-bottom: none;
  text-align: left;
  font-style: italic;
}

/* Significance stars */
.sig-star { color: var(--primary); font-weight: 700; }

/* Highlighted cells in factor/correlation tables */
.result-table td.hl { font-weight: 600; }

/* Warning / info boxes in results */
.result-warning {
  display: flex;
  gap: var(--sp-sm);
  padding: var(--sp-sm) var(--sp-md);
  border-radius: var(--radius-md);
  font-size: var(--font-size-sm);
  margin-bottom: var(--sp-sm);
}

.result-warning.warn {
  background: #fffbeb;
  border: 1px solid #fcd34d;
  color: #92400e;
}

.result-warning.error {
  background: #fef2f2;
  border: 1px solid #fca5a5;
  color: #991b1b;
}

.result-warning.info {
  background: var(--primary-light);
  border: 1px solid #93c5fd;
  color: #1e40af;
}

/* ── Text / Number Input ────────────────────────────────────────── */
.input {
  width: 100%;
  padding: 5px var(--sp-sm);
  font-family: var(--font);
  font-size: var(--font-size-sm);
  border: 1px solid var(--border-dark);
  border-radius: var(--radius-sm);
  color: var(--text);
  background: var(--bg-panel);
  outline: none;
}

.input:focus { border-color: var(--primary); }

/* ── Variable Selector active state ────────────────────────────── */
.var-selector.active {
  border-color: var(--primary);
  background: var(--primary-light);
}

/* Sub-config panel within analysis config body */
.sub-config { }

/* Result table footnote */
.result-table-note {
  padding: var(--sp-sm) var(--sp-md);
  font-size: var(--font-size-xs);
  color: var(--text-muted);
  font-style: italic;
  border-top: 1px solid var(--border);
}

/* Export All bar above results */
.output-export-bar {
  display: flex;
  align-items: center;
  gap: var(--sp-xs);
  padding: var(--sp-xs) var(--sp-md);
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  margin-bottom: var(--sp-md);
}
.output-export-label {
  font-size: var(--font-size-xs);
  color: var(--text-muted);
  margin-right: auto;
}

/* Charts row inside result section */
.result-charts-row {
  padding: var(--sp-md);
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-md);
}

/* Correlation matrix table cells */
.corr-matrix td.pos-high  { background: #dbeafe; color: #1e40af; font-weight: 600; }
.corr-matrix td.neg-high  { background: #fee2e2; color: #991b1b; font-weight: 600; }
.corr-matrix td.diag      { background: var(--bg-sidebar); color: var(--text-muted); }

/* Iteration log (logistic regression) */
.iteration-log {
  background: #0f172a;
  color: #94a3b8;
  font-family: var(--font-mono);
  font-size: 11px;
  padding: var(--sp-sm) var(--sp-md);
  border-radius: var(--radius-md);
  max-height: 180px;
  overflow-y: auto;
  line-height: 1.6;
}

.iteration-log .log-converged { color: #4ade80; }
.iteration-log .log-warn      { color: #fbbf24; }
.iteration-log .log-error     { color: #f87171; }
