/* ---------- Theme tokens ---------- */
:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f0f1f4;
  --border: #e2e5ea;
  --border-strong: #d0d4db;
  --text: #1c2024;
  --text-soft: #626973;
  --text-faint: #8b929c;
  --accent: #4f46e5;
  --accent-soft: #ecebfd;
  --accent-text: #ffffff;
  --danger: #dc2626;
  --shadow: 0 1px 2px rgba(18, 22, 31, .06), 0 4px 16px rgba(18, 22, 31, .06);
  --radius: 12px;
  --radius-sm: 8px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

:root[data-theme="dark"] {
  --bg: #16181d;
  --surface: #1e2127;
  --surface-2: #262a31;
  --border: #2e333b;
  --border-strong: #3a4049;
  --text: #e7e9ec;
  --text-soft: #a3aab4;
  --text-faint: #6d7581;
  --accent: #8b7ff0;
  --accent-soft: #2a2942;
  --accent-text: #ffffff;
  --danger: #f87171;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.28);
}

@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] {
    --bg: #16181d;
    --surface: #1e2127;
    --surface-2: #262a31;
    --border: #2e333b;
    --border-strong: #3a4049;
    --text: #e7e9ec;
    --text-soft: #a3aab4;
    --text-faint: #6d7581;
    --accent: #8b7ff0;
    --accent-soft: #2a2942;
    --accent-text: #ffffff;
    --danger: #f87171;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.28);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.5;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

/* ---------- Header ---------- */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  width: 34px; height: 34px;
  display: grid; place-items: center;
  background: var(--accent);
  color: var(--accent-text);
  border-radius: 9px;
  font-size: 18px;
  font-weight: 700;
}
.brand-text h1 { margin: 0; font-size: 15px; font-weight: 650; letter-spacing: -.01em; }
.brand-sub { font-size: 11px; color: var(--text-faint); text-transform: uppercase; letter-spacing: .08em; }
.header-actions { display: flex; align-items: center; gap: 8px; }
.header-left { display: flex; align-items: center; gap: 10px; min-width: 0; }

/* Bigger settings gear on desktop */
@media (min-width: 861px) {
  #settings-btn { font-size: 20px; padding: 5px 11px; line-height: 1; }
}
.nav-mobile { display: none; }
.nav-title {
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  background: transparent;
  border: 0;
  cursor: pointer;
  padding: 6px 8px;
  border-radius: 7px;
  max-width: 48vw;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.nav-title::after { content: ' ⌄'; color: var(--text-faint); }
.nav-title:hover { background: var(--surface-2); }
.drawer-overlay { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); z-index: 55; display: none; }
.header-link { text-decoration: none; font-size: 13px; }
.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
.settings-links { display: flex; flex-wrap: wrap; gap: 8px; }
.settings-links .btn { text-decoration: none; }

/* Onboarding */
.onboard-panel {
  width: min(520px, 92vw);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .4);
  padding: 28px;
  text-align: center;
}
.onboard-mark {
  display: inline-grid;
  place-items: center;
  width: 52px; height: 52px;
  background: var(--accent);
  color: var(--accent-text);
  border-radius: 13px;
  font-size: 27px;
  font-weight: 700;
  margin-bottom: 14px;
}
.onboard-panel h2 { margin: 0 0 6px; font-size: 20px; }
.onboard-lead { margin: 0 0 18px; color: var(--text-soft); font-size: 14px; }
.onboard-steps {
  text-align: left;
  margin: 0 0 22px;
  padding-left: 22px;
  display: flex;
  flex-direction: column;
  gap: 9px;
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
}
.onboard-steps li::marker { color: var(--accent); font-weight: 700; }
.onboard-actions { display: flex; justify-content: center; gap: 10px; }
.onboard-actions .btn { text-decoration: none; }

/* ---------- Buttons ---------- */
.btn {
  font-family: inherit;
  font-size: 13px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 7px 13px;
  cursor: pointer;
  transition: background .12s, border-color .12s, transform .05s;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-primary:hover { filter: brightness(1.07); background: var(--accent); }
.btn-ghost { border-color: transparent; background: transparent; color: var(--text-soft); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; align-self: flex-start; }
.btn-danger:hover { filter: brightness(1.07); background: var(--danger); }
.btn-mini { padding: 3px 9px; font-size: 12px; border-radius: 6px; }
.btn-icon { padding: 4px 8px; }

/* ---------- Main layout ---------- */
.app-body {
  display: flex;
  flex: 1;
  min-height: 0;
}

/* ---------- Sidebar ---------- */
.sidebar {
  width: 244px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.sidebar-tabs {
  display: flex;
  gap: 6px;
  padding: 10px 12px 8px;
}
.sidebar-tab {
  flex: 1;
  padding: 6px 0;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text-soft);
  border-radius: 7px;
  font: inherit;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
}
.sidebar-tab:hover { background: var(--surface-2); }
.sidebar-tab.active { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.sidebar-search-input {
  margin: 0 12px 8px;
  padding: 7px 10px;
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: 13px;
  outline: none;
}
.sidebar-search-input:focus { border-color: var(--accent); }
.side-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.side-list[hidden] { display: none; }
.side-list > * { flex-shrink: 0; }  /* don't squish items when the list overflows — scroll instead */
.side-action { color: var(--text); }
.side-cat {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--text-faint);
  padding: 11px 10px 4px;
  font-weight: 600;
}
.side-action {
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.side-action:hover { background: var(--accent-soft); }
.side-empty { padding: 20px 12px; text-align: center; color: var(--text-faint); font-size: 12.5px; }
.sidebar-foot {
  padding: 8px 12px;
  border-top: 1px solid var(--border);
}
.sidebar-foot .btn { width: 100%; }
.wf-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  border: 1px solid transparent;
}
.wf-item:hover { background: var(--surface-2); }
.wf-item.active { background: var(--accent-soft); }
.wf-item.active .wf-name { color: var(--accent); }
.wf-name {
  flex: 1;
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.wf-name-input {
  flex: 1;
  font: inherit;
  font-size: 13px;
  padding: 2px 5px;
  border: 1px solid var(--accent);
  border-radius: 5px;
  background: var(--surface);
  color: var(--text);
  outline: none;
}
.wf-count { font-size: 11px; color: var(--text-faint); font-variant-numeric: tabular-nums; }
.wf-del {
  border: 0;
  background: transparent;
  color: var(--text-faint);
  cursor: pointer;
  font-size: 13px;
  padding: 0 2px;
  line-height: 1;
  opacity: 0;
}
.wf-item:hover .wf-del { opacity: .7; }
.wf-del:hover { color: var(--danger); opacity: 1; }

/* ---------- 3-column work area ---------- */
.work-area {
  flex: 1;
  display: flex;
  min-width: 0;
  min-height: 0;
}
.actions-col {
  width: 300px;
  flex: 0 0 300px;
  min-width: 0;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--bg);
  border-right: 1px solid var(--border);
}
.io-col {
  flex: 1;
  min-width: 320px;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.col-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 11px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.col-title {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.col-head-right { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.btn-icon { padding: 4px 9px; font-size: 15px; line-height: 1; }

.menu-wrap { position: relative; }
.menu {
  position: absolute;
  top: calc(100% + 5px);
  right: 0;
  min-width: 230px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  padding: 4px;
  z-index: 40;
}
.menu[hidden] { display: none; }
.menu-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 8px 10px;
  border: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 13px;
  border-radius: 6px;
  cursor: pointer;
  white-space: nowrap;
}
.menu-item:hover { background: var(--accent-soft); }

.side-action.active { background: var(--accent-soft); color: var(--accent); }
.actions-col .pipeline {
  flex: 1;
  overflow-y: auto;
}

/* io panels fill their half, editors grow */
.io-panel {
  flex: 1;
  min-height: 0;
  border: 0;
  border-radius: 0;
  box-shadow: none;
}
.io-panel + .io-panel { border-top: 1px solid var(--border); }
.io-col .editor,
.io-col .preview-frame {
  flex: 1;
  min-height: 0;
  resize: none;
}

.io-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}
.panel-title { font-weight: 600; font-size: 13px; }
.panel-tools { display: flex; align-items: center; gap: 8px; }
.counter { font-size: 11px; color: var(--text-faint); font-variant-numeric: tabular-nums; }

.editor {
  border: 0;
  resize: vertical;
  width: 100%;
  min-height: 240px;
  padding: 14px;
  background: transparent;
  color: var(--text);
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.6;
  outline: none;
}
.editor::placeholder { color: var(--text-faint); }

.preview-frame {
  border: 0;
  width: 100%;
  min-height: 240px;
  background: #fff;
  display: block;
}
.preview-frame[hidden] { display: none; }

/* ---------- Workflow ---------- */
.workflow {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.workflow-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}
.workflow-hint { color: var(--text-faint); font-size: 12px; flex: 1; }
.workflow-head .panel-title { flex-shrink: 0; }

.pipeline {
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 60px;
}
.pipeline-empty {
  text-align: center;
  color: var(--text-faint);
  padding: 26px 10px;
  font-size: 13px;
}

/* ---------- Action card ---------- */
.action-card {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  overflow: hidden;
}
.action-card.disabled { opacity: .55; }
.action-card-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 11px;
  background: var(--surface-2);
}
.action-step {
  width: 22px; height: 22px;
  flex-shrink: 0;
  display: grid; place-items: center;
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: 6px;
  font-size: 12px;
  font-weight: 700;
}
.action-name { font-weight: 600; font-size: 13px; flex: 1; }
.action-card-controls { display: flex; align-items: center; gap: 2px; }
.action-card-controls .btn { padding: 3px 7px; font-size: 12px; color: var(--text-soft); border-color: transparent; background: transparent; }
.action-card-controls .btn:hover:not(:disabled) { background: var(--surface); }
.action-card-controls .btn:disabled { opacity: .3; cursor: default; }
.action-card-controls .btn-remove:hover { color: var(--danger); }

.action-options {
  padding: 11px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 10px 14px;
}
.action-options:empty { display: none; }
.opt { display: flex; flex-direction: column; gap: 4px; }
.opt-full { grid-column: 1 / -1; }
.opt label { font-size: 11px; color: var(--text-soft); font-weight: 500; }
.opt input[type="text"],
.opt input[type="number"],
.opt select,
.opt textarea {
  font-family: inherit;
  font-size: 13px;
  padding: 6px 9px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  outline: none;
  width: 100%;
}
.opt textarea { font-family: var(--mono); resize: vertical; min-height: 54px; }
.opt input:focus, .opt select:focus, .opt textarea:focus { border-color: var(--accent); }
.opt-check { flex-direction: row; align-items: center; gap: 8px; }
.opt-check input { width: 15px; height: 15px; accent-color: var(--accent); }
.opt-check label { font-size: 12px; color: var(--text); }

/* toggle switch */
.switch { position: relative; width: 34px; height: 20px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .slider {
  position: absolute; inset: 0;
  background: var(--border-strong);
  border-radius: 999px;
  transition: background .15s;
}
.switch .slider::before {
  content: ""; position: absolute;
  height: 14px; width: 14px; left: 3px; top: 3px;
  background: #fff; border-radius: 50%;
  transition: transform .15s;
}
.switch input:checked + .slider { background: var(--accent); }
.switch input:checked + .slider::before { transform: translateX(14px); }

/* ---------- Palette ---------- */
.palette-overlay {
  position: fixed; inset: 0;
  background: rgba(15, 18, 24, .45);
  backdrop-filter: blur(2px);
  display: grid;
  place-items: start center;
  padding-top: 8vh;
  z-index: 90; /* above the mobile drawers (60) and header (70) so modals sit on top */
}
.palette-overlay[hidden] { display: none; }
.palette {
  width: min(640px, 92vw);
  max-height: 78vh;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(0,0,0,.35);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.palette-head {
  display: flex;
  gap: 8px;
  padding: 12px;
  border-bottom: 1px solid var(--border);
}
.palette-search {
  flex: 1;
  font-family: inherit;
  font-size: 15px;
  padding: 9px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--text);
  outline: none;
}
.palette-search:focus { border-color: var(--accent); }
.palette-list { overflow-y: auto; padding: 8px; }
.palette-group-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--text-faint);
  padding: 12px 10px 5px;
  font-weight: 600;
}
.palette-item {
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding: 9px 11px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  border: 1px solid transparent;
}
.palette-item:hover, .palette-item.active {
  background: var(--accent-soft);
  border-color: transparent;
}
.palette-item-name { font-weight: 600; font-size: 13px; }
.palette-item-desc { font-size: 12px; color: var(--text-soft); }
.palette-empty { padding: 30px; text-align: center; color: var(--text-faint); }

/* ---------- Manual action control (Run / Speak) ---------- */
.action-control {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 10px;
  padding-top: 4px;
  flex-wrap: wrap;
}
.action-control .btn { font-size: 13px; }
.control-status { font-size: 12px; color: var(--text-faint); }
.control-status.err { color: var(--danger); }
.control-status.ok { color: #16a34a; }
.spinner {
  width: 13px; height: 13px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  display: inline-block;
  vertical-align: -2px;
  animation: spin .7s linear infinite;
  margin-right: 6px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Settings ---------- */
.settings-panel {
  width: min(520px, 92vw);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(0,0,0,.35);
  overflow: hidden;
}
.settings-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.settings-body { padding: 16px; display: flex; flex-direction: column; gap: 14px; }
.settings-note { margin: 0; font-size: 12.5px; color: var(--text-soft); line-height: 1.5; }
.settings-field { display: flex; flex-direction: column; gap: 5px; }
.settings-field span { font-size: 12px; color: var(--text-soft); font-weight: 500; }
.settings-field input {
  font-family: inherit;
  font-size: 14px;
  padding: 9px 11px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--text);
  outline: none;
}
.settings-field input:focus { border-color: var(--accent); }
.settings-actions { display: flex; justify-content: flex-end; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 9px 16px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 500;
  box-shadow: 0 8px 24px rgba(0,0,0,.3);
  z-index: 100;
  animation: toast-in .2s ease;
}
.toast[hidden] { display: none; }
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 8px); } }

/* ---------- Responsive ---------- */
/* ---------- Mobile: only Input/Result visible; sidebar + actions become drawers ---------- */
@media (max-width: 860px) {
  .brand { display: none; }
  .desktop-only { display: none; }
  .nav-mobile { display: inline-flex; align-items: center; }
  #nav-menu-btn { font-size: 26px; padding: 2px 10px; line-height: 1; }
  .nav-title { pointer-events: none; }        /* it's a nav-bar title, not a button */
  .nav-title::after { content: none; }
  .app-header { height: 56px; z-index: 70; }
  .drawer-overlay { display: none; }

  /* iOS-style navigation stack: List (sidebar) ⟷ Detail (work-area) */
  .app-body { position: relative; overflow: hidden; }
  .sidebar,
  .work-area {
    position: absolute;
    inset: 0;
    width: auto;
    transition: transform .25s ease;
    will-change: transform;
  }
  .sidebar { transform: translateX(-100%); border-right: 0; }
  .work-area { transform: translateX(0); flex-direction: column; overflow-y: auto; }
  .app-body.show-list .sidebar { transform: translateX(0); }
  .app-body.show-list .work-area { transform: translateX(100%); }

  /* the Detail screen stacks the steps above the input/result and scrolls as one */
  .actions-col,
  .io-col {
    position: static;
    transform: none;
    width: auto;
    flex: none;
    min-width: 0;
    border-right: 0;
    box-shadow: none;
  }
  .actions-col { border-bottom: 1px solid var(--border); }
  .actions-col .pipeline { overflow: visible; }
  .io-panel { flex: none; }
  .io-col .editor,
  .io-col .preview-frame { min-height: 220px; flex: none; }
}
