:root {
  --bg: #13151a;
  --surface: #1c1f27;
  --surface-2: #21242e;
  --border: #2a2d3a;
  --text: #dde1f0;
  --muted: #a0a6c4;
  --accent: #5b8eff;
  --accent-dim: #3a5cba;
  --ok: #4ec97b;
  --error: #e05c5c;
  --warn: #d49b3f;
  --code-bg: #0f1117;
  --radius: 5px;
  --radius-lg: 8px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

body { display: flex; flex-direction: column; }

/* ── Header ── */
header {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0 1rem;
  height: 44px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.logo {
  font-family: 'JetBrains Mono', 'Fira Code', monospace;
  font-weight: 700;
  font-size: 0.95rem;
  color: var(--accent);
  letter-spacing: -0.02em;
}

.exec-toggle {
  display: flex;
  gap: 0.1rem;
  margin-left: auto;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2px;
}

.radio-label {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.25rem 0.65rem;
  border-radius: 3px;
  cursor: pointer;
  font-size: 0.8rem;
  color: var(--muted);
  transition: color 0.15s, background 0.15s;
  user-select: none;
}

.radio-label:has(input:checked) {
  background: var(--surface-2);
  color: var(--text);
}

.radio-label input { display: none; }

.pyodide-status {
  font-size: 0.8rem;
  padding: 0.2rem 0.55rem;
  border-radius: var(--radius);
  white-space: nowrap;
}
.pyodide-status.loading { color: var(--muted); }
.pyodide-status.ready   { color: var(--ok);   }
.pyodide-status.error   { color: var(--error); }

/* ── Main layout ── */
main {
  flex: 1;
  display: grid;
  grid-template-columns: 400px 1fr;
  overflow: hidden;
  height: calc(100vh - 44px);
}

.panel {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  padding: 0.85rem;
  overflow-y: auto;
}

.left-panel {
  border-right: 1px solid var(--border);
  background: var(--bg);
}

.right-panel {
  background: var(--bg);
}

/* ── Labels ── */
.pane-label {
  font-size: 0.74rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.hint {
  font-size: 0.76rem;
  color: var(--muted);
}

/* ── Textareas ── */
textarea {
  width: 100%;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  color: var(--text);
  font-family: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace;
  font-size: 0.78rem;
  line-height: 1.55;
  padding: 0.7rem 0.85rem;
  resize: vertical;
  min-height: 180px;
  transition: border-color 0.15s;
}

textarea:focus { outline: none; border-color: var(--accent); }
textarea::placeholder { color: var(--muted); opacity: 0.5; }

#input  { min-height: 180px; height: 220px; }
#output { flex: 1; min-height: 300px; height: 100%; resize: none; }

/* ── Pipeline section ── */
.pipeline-section { display: flex; flex-direction: column; gap: 0.4rem; }

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.stage-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.stage-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  user-select: none;
  transition: border-color 0.1s, opacity 0.15s;
}

.stage-item.dragging { opacity: 0.3; border-style: dashed; }
.stage-item.drag-over { border-color: var(--accent); }

.stage-pinned {
  border-left: 2px solid var(--accent-dim);
  background: var(--surface-2);
}

.stage-item.disabled { opacity: 0.5; }

.drag-handle-spacer {
  display: inline-block;
  width: 1em;
  flex-shrink: 0;
}

.stage-lock {
  font-size: 0.75rem;
  opacity: 0.7;
  flex-shrink: 0;
}

.stage-item-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 0.85rem;
  line-height: 1;
  padding: 2px 4px;
  border-radius: 3px;
  color: var(--muted);
  flex-shrink: 0;
  transition: color 0.12s, background 0.12s;
}
.stage-item-remove:hover { color: var(--error); background: color-mix(in srgb, var(--error) 12%, transparent); }
.stage-item-dup:hover    { color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, transparent); }

.stage-toggle-btn { font-size: 1rem; padding: 2px 3px; }
.stage-toggle-btn:hover { color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, transparent); }

#add-step-row {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.3rem 0 0.1rem;
}

#add-step-row select {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 0.28rem 0.5rem;
  font-size: 0.78rem;
  cursor: pointer;
}
#add-step-row select:focus { outline: none; border-color: var(--accent); }

.stage-row {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.38rem 0.55rem;
}

.drag-handle {
  color: var(--muted);
  cursor: grab;
  font-size: 0.95rem;
  line-height: 1;
  opacity: 0.7;
  flex-shrink: 0;
}
.drag-handle:active { cursor: grabbing; }

.stage-toggle {
  display: flex;
  align-items: center;
  flex-shrink: 0;
}
.stage-toggle input { cursor: pointer; accent-color: var(--accent); }

.stage-label-group {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.08rem;
  min-width: 0;
}

.stage-label {
  font-size: 0.88rem;
  font-weight: 500;
}

.stage-summary {
  font-size: 0.78rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.expand-btn {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 0.78rem;
  padding: 2px 5px;
  border-radius: 3px;
  line-height: 1;
}
.expand-btn:hover { color: var(--text); background: var(--surface-2); }

/* ── Preset row ── */
.preset-row {
  display: flex;
  gap: 0.25rem;
  flex-wrap: wrap;
  padding-bottom: 0.35rem;
  border-bottom: 1px solid var(--border);
  margin-bottom: 0.15rem;
}

.preset-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--muted);
  cursor: pointer;
  font-size: 0.78rem;
  padding: 0.18rem 0.55rem;
  transition: border-color 0.12s, color 0.12s;
}
.preset-btn:hover { border-color: var(--accent); color: var(--text); }
.preset-btn.active {
  border-color: var(--accent);
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, transparent);
}

/* ── Strategy chips ── */
.chip-row { flex-wrap: wrap; gap: 0.3rem; align-items: center; }

.stage-chip {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
  cursor: pointer;
  font-size: 0.78rem;
  padding: 0.18rem 0.6rem;
  line-height: 1.4;
  transition: border-color 0.12s, color 0.12s, background 0.12s;
  white-space: nowrap;
  user-select: none;
}
.stage-chip:hover { border-color: var(--accent); color: var(--text); }
.stage-chip.active {
  background: color-mix(in srgb, var(--accent) 18%, var(--surface-2));
  border-color: var(--accent);
  color: var(--accent);
}

/* ── Stage config panel ── */
.stage-config {
  padding: 0.55rem 0.7rem;
  border-top: 1px solid var(--border);
  background: var(--code-bg);
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.config-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.82rem;
}

.config-row > .row-label {
  color: var(--muted);
  flex-shrink: 0;
  font-size: 0.82rem;
}

.check-label {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  cursor: pointer;
  white-space: nowrap;
  font-size: 0.82rem;
  color: var(--text);
}
.check-label:hover { opacity: 1; }
.check-label input { accent-color: var(--accent); }

input[type="range"] {
  accent-color: var(--accent);
  width: 110px;
  vertical-align: middle;
}

.range-val {
  font-family: monospace;
  font-size: 0.82rem;
  color: var(--accent);
  min-width: 2.5ch;
}

/* ── Options section ── */
.options-section { display: flex; flex-direction: column; gap: 0.55rem; }

.seed-row, .upload-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.seed-row input[type="number"] {
  width: 90px;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 0.28rem 0.5rem;
  font-size: 0.82rem;
  min-height: 0;
  resize: none;
}
.seed-row input[type="number"]:focus { outline: none; border-color: var(--accent); }

input[type="file"] {
  font-size: 0.82rem;
  color: var(--muted);
  cursor: pointer;
}

.upload-status {
  font-size: 0.8rem;
  width: 100%;
}
.upload-status.ok    { color: var(--ok); }
.upload-status.error { color: var(--error); }

/* ── Buttons ── */
.btn-primary {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: 0.5rem 1.1rem;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
  align-self: flex-start;
}
.btn-primary:hover:not(:disabled) { background: var(--accent-dim); }
.btn-primary:disabled { opacity: 0.35; cursor: not-allowed; }

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.28rem 0.75rem;
  font-size: 0.78rem;
  cursor: pointer;
  transition: border-color 0.15s;
}
.btn-secondary:hover:not(:disabled) { border-color: var(--accent); }
.btn-secondary:disabled { opacity: 0.3; cursor: not-allowed; }

.key-hint { align-self: flex-start; }

/* ── Output panel ── */
.output-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.error-msg {
  color: var(--error);
  font-size: 0.78rem;
  white-space: pre-wrap;
  font-family: monospace;
  line-height: 1.4;
}

/* ── Help button ── */
.help-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: 50%;
  color: var(--muted);
  cursor: pointer;
  font-size: 0.75rem;
  font-weight: 700;
  width: 22px;
  height: 22px;
  line-height: 1;
  padding: 0;
  flex-shrink: 0;
  transition: border-color 0.12s, color 0.12s;
}
.help-btn:hover { border-color: var(--accent); color: var(--accent); }

/* ── Modal ── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal-overlay[hidden] { display: none; }

.modal-dialog {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.5rem;
  max-width: 540px;
  width: 90%;
  max-height: 90vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-title {
  font-family: 'JetBrains Mono', 'Fira Code', monospace;
  font-weight: 700;
  font-size: 1rem;
  color: var(--accent);
  letter-spacing: -0.02em;
}

.modal-close {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 1.2rem;
  line-height: 1;
  padding: 2px 5px;
  border-radius: var(--radius);
  transition: color 0.12s;
}
.modal-close:hover { color: var(--text); }

.modal-body {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  font-size: 0.85rem;
  line-height: 1.55;
}

.modal-pitch {
  color: var(--text);
  font-weight: 500;
}

.modal-features {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  padding: 0;
}

.modal-features li {
  font-size: 0.84rem;
  line-height: 1.55;
  color: var(--muted);
  padding-left: 1rem;
  position: relative;
}

.modal-features li::before {
  content: '—';
  position: absolute;
  left: 0;
  color: var(--border);
}

.modal-features li strong {
  color: var(--text);
  font-weight: 600;
}

.modal-section-label {
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--muted);
  margin-bottom: -0.25rem;
}

.modal-steps {
  padding-left: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}
.modal-steps li { color: var(--text); }
.modal-steps strong { color: var(--text); font-weight: 600; }
.modal-steps kbd {
  font-family: 'JetBrains Mono', 'Fira Code', monospace;
  font-size: 0.76rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 0 0.3rem;
  color: var(--text);
}

.modal-note {
  color: var(--muted);
  font-size: 0.8rem;
  line-height: 1.55;
  border-left: 2px solid var(--border);
  padding-left: 0.65rem;
}

.modal-links {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}

.modal-link {
  color: var(--accent);
  text-decoration: none;
  font-size: 0.84rem;
}
.modal-link:hover { text-decoration: underline; }

.modal-credit {
  font-size: 0.8rem;
  color: var(--muted);
}
.modal-credit .modal-link { font-size: inherit; }

.modal-footer {
  border-top: 1px solid var(--border);
  padding-top: 0.75rem;
}

/* ── Stage description ── */
.stage-desc {
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.4;
}

/* ── Custom strategy templates ── */
.template-details {
  width: 100%;
}
.template-details > summary {
  cursor: pointer;
  list-style: none;
  user-select: none;
  padding: 0.3rem 0.55rem;
  font-size: 0.78rem;
  font-weight: 500;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  transition: border-color 0.12s, color 0.12s;
}
.template-details > summary:hover { border-color: var(--accent); color: var(--accent); }
.template-details[open] > summary { border-color: var(--accent); color: var(--accent); }
.template-details > summary::-webkit-details-marker { display: none; }
.template-details > summary::before { content: '▸  '; }
.template-details[open] > summary::before { content: '▾  '; }

.template-content {
  margin-top: 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.tmpl-label {
  font-size: 0.74rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin-top: 0.35rem;
}
.tmpl-label:first-child { margin-top: 0; }

.template-content pre {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.6rem 0.75rem;
  overflow-x: auto;
  font-size: 0.78rem;
  line-height: 1.5;
  font-family: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace;
  color: var(--text);
  white-space: pre;
}

/* ── Mobile layout ── */
@media (max-width: 768px) {
  main {
    grid-template-columns: 1fr;
    height: auto;
    overflow-y: auto;
  }
  .left-panel {
    border-right: none;
    border-bottom: 1px solid var(--border);
    overflow-y: visible;
  }
  .right-panel { overflow-y: visible; }

  header { flex-wrap: wrap; height: auto; padding: 0.5rem 0.75rem; }

  .btn-primary,
  .btn-secondary { min-height: 44px; padding: 0.6rem 1.1rem; }

  .stage-row { min-height: 44px; }
  .expand-btn { min-height: 44px; min-width: 44px; padding: 0; }
  .drag-handle { min-height: 44px; display: flex; align-items: center; padding: 0 0.4rem; }

  input[type="range"] { width: 100%; height: 44px; }
}
