/* ==================================================================
   AI SQL Assistant — local in-browser model panel for the Custom SQL step
   ==================================================================
   Injected into the Custom SQL step dialog (`.modal.step-dialog--large`).
   Adds a right-side "Local AI" column with the 3 in-browser providers
   (wllama / WebLLM / Transformers.js), download + install, and a
   "Generate with Local AI" flow that writes DuckDB SQL.

   Uses only root design tokens from assets/css/base.css. No shared
   component classes are redefined.
   ================================================================== */

/* --- Widen the Custom SQL dialog to make room for the panel --------- */
.modal.step-dialog--large.etl-ai-active {
  position: relative;
  width: min(1280px, 96vw);
  padding-right: 320px;
}

/* --- The panel: right-side column inside the dialog ----------------- */
.etl-ai-local {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: 320px;
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  padding: var(--space-md);
  background: var(--color-surface);
  border-left: 1px solid var(--color-border);
  overflow-y: auto;
  z-index: 5;
}

/* --- Header ---------------------------------------------------------- */
.etl-ai-local-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-sm);
  flex-wrap: wrap;
}
.etl-ai-local-title {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--color-text);
}
.etl-ai-privacy {
  display: inline-flex;
  align-items: center;
  gap: var(--space-xs);
  font-size: 11px;
  font-weight: 600;
  color: var(--color-success);
  border: 1px solid color-mix(in srgb, var(--color-success) 40%, transparent);
  border-radius: 10px;
  padding: 2px 8px;
  white-space: nowrap;
}

/* --- Fields ---------------------------------------------------------- */
.etl-ai-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.etl-ai-field-label {
  font-size: var(--text-xs);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-text-secondary);
}
.etl-ai-select {
  padding: 6px 8px;
  font-size: var(--text-sm);
  border: 1px solid var(--color-border);
  border-radius: var(--border-radius-sm);
  background: var(--color-bg);
  color: var(--color-text);
  transition: border-color 200ms ease, box-shadow 200ms ease;
}
.etl-ai-select:focus {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--color-accent) 20%, transparent);
}

/* --- Model meta line ------------------------------------------------- */
.etl-ai-model-meta {
  font-size: var(--text-xs);
  color: var(--color-text-secondary);
  line-height: 1.4;
}
.etl-ai-model-meta .etl-ai-hw {
  display: block;
  margin-top: 2px;
  opacity: 0.85;
}

/* --- Buttons --------------------------------------------------------- */
.etl-ai-btn {
  padding: 8px 12px;
  font-size: var(--text-sm);
  font-weight: 600;
  border: 1px solid var(--color-border);
  border-radius: var(--border-radius-sm);
  background: var(--color-bg);
  color: var(--color-text);
  cursor: pointer;
  transition: background 200ms ease, border-color 200ms ease, color 200ms ease;
}
.etl-ai-btn:hover {
  border-color: var(--color-accent);
  color: var(--color-accent);
}
.etl-ai-btn:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}
.etl-ai-btn:disabled {
  opacity: 0.4;
  pointer-events: none;
}
.etl-ai-btn.primary {
  background: var(--color-accent);
  border-color: var(--color-accent);
  color: var(--color-bg);
}
.etl-ai-btn.primary:hover {
  background: var(--color-accent-hover);
  border-color: var(--color-accent-hover);
  color: var(--color-bg);
}
.etl-ai-btn.ghost {
  background: transparent;
  border-color: transparent;
  color: var(--color-text-secondary);
}
.etl-ai-btn.ghost:hover {
  color: var(--color-accent);
  border-color: var(--color-border);
}

/* --- Download progress ------------------------------------------------ */
.etl-ai-progress {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.etl-ai-progress-bar {
  height: 6px;
  border-radius: 3px;
  background: var(--color-border);
  overflow: hidden;
}
.etl-ai-progress-fill {
  height: 100%;
  width: 0;
  border-radius: 3px;
  background: var(--color-accent);
  transition: width 200ms ease;
}
.etl-ai-progress-text {
  font-size: var(--text-xs);
  color: var(--color-text-secondary);
  font-variant-numeric: tabular-nums;
}

/* --- Column quick-insert buttons -------------------------------------- */
.etl-ai-columns {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.etl-ai-columns-label {
  font-size: var(--text-xs);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-text-secondary);
}
.etl-ai-columns-list {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  max-height: 96px;
  overflow-y: auto;
}
.etl-ai-col-btn {
  padding: 3px 8px;
  font-size: 11px;
  font-family: var(--font-mono);
  border: 1px solid var(--color-border);
  border-radius: var(--border-radius-sm);
  background: var(--color-bg);
  color: var(--color-text-secondary);
  cursor: pointer;
  transition: border-color 200ms ease, color 200ms ease, background 200ms ease;
}
.etl-ai-col-btn:hover {
  border-color: var(--color-accent);
  color: var(--color-accent);
  background: color-mix(in srgb, var(--color-accent) 8%, transparent);
}
.etl-ai-col-btn:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}

/* --- Generate button --------------------------------------------------- */
.etl-ai-generate {
  width: 100%;
}

/* --- Result ------------------------------------------------------------- */
.etl-ai-result {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  border: 1px solid var(--color-border);
  border-radius: var(--border-radius-md);
  background: var(--color-bg);
  overflow: hidden;
}
.etl-ai-result-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-sm);
  padding: 6px 10px;
  border-bottom: 1px solid var(--color-border);
  background: var(--color-surface);
}
.etl-ai-result-title {
  font-size: var(--text-xs);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-text-secondary);
}
.etl-ai-result-sql {
  margin: 0;
  padding: 10px;
  max-height: 220px;
  overflow: auto;
  font-family: var(--font-mono);
  font-size: 11px;
  line-height: 1.5;
  color: var(--color-text);
  white-space: pre-wrap;
  word-break: break-word;
}
.etl-ai-result-actions {
  display: flex;
  gap: var(--space-sm);
  padding: 0 10px 10px;
}
.etl-ai-result-actions .etl-ai-btn {
  flex: 1;
  padding: 6px 10px;
  font-size: var(--text-xs);
}

/* --- Status / error line ------------------------------------------------ */
.etl-ai-status {
  font-size: var(--text-xs);
  color: var(--color-text-secondary);
  line-height: 1.4;
}
.etl-ai-status.is-error {
  color: var(--color-error);
}

/* --- Dark mode: panel surface already uses tokens; keep borders crisp --- */
[data-theme="dark"] .etl-ai-local {
  border-left-color: var(--color-border);
}
[data-theme="dark"] .etl-ai-result {
  border-color: var(--color-border);
}

/* --- Reduced motion: no width animation on the progress bar ------------ */
@media (prefers-reduced-motion: reduce) {
  .etl-ai-progress-fill {
    transition: none;
  }
}

/* --- Narrow screens: collapse the panel to a bottom sheet -------------- */
@media (max-width: 1100px) {
  .modal.step-dialog--large.etl-ai-active {
    width: min(960px, 95vw);
    padding-right: 0;
  }
  .etl-ai-local {
    position: fixed;
    top: auto;
    right: 0;
    bottom: 0;
    left: 0;
    width: auto;
    max-height: 45vh;
    border-left: none;
    border-top: 1px solid var(--color-border);
    box-shadow: var(--shadow-lg);
    z-index: 200;
  }
}
