:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --border: #dfe3e8;
  --text: #1d2430;
  --muted: #6b7686;
  --accent: #1f6feb;
  --accent-soft: #e7f0ff;
  --header-row: #fff4d6;
  --skipped: #f0f1f3;
  --danger: #c93c37;
  --new-col: #e6f6ea;
  --grid-head: #f1f3f6;
  --shadow: 0 6px 24px rgba(20, 30, 50, .12);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  font-size: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171c; --panel: #1b1f26; --border: #2c323c; --text: #e3e7ee; --muted: #8d97a7;
    --accent: #4c8dff; --accent-soft: #1d2b44; --header-row: #3a3220; --skipped: #20242b;
    --danger: #ff6b66; --new-col: #1f3527; --grid-head: #222831; --shadow: 0 6px 24px rgba(0,0,0,.5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--bg); color: var(--text); height: 100vh; display: flex; flex-direction: column; }
button, input, select { font: inherit; color: inherit; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 18px; background: var(--panel); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 600; font-size: 16px; }
.logo { color: var(--accent); }

.btn {
  display: inline-flex; align-items: center; gap: 6px; cursor: pointer;
  padding: 6px 14px; border-radius: 6px; border: 1px solid var(--border); background: var(--panel);
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn {
  border: none; background: none; cursor: pointer; color: var(--muted);
  padding: 2px 6px; border-radius: 4px; visibility: hidden;
}
.icon-btn:hover { color: var(--danger); background: var(--skipped); }

.layout { flex: 1; display: flex; min-height: 0; }
.sidebar {
  width: 280px; flex-shrink: 0; overflow-y: auto; padding: 12px;
  background: var(--panel); border-right: 1px solid var(--border);
}
.sidebar h2 { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 10px 6px 6px; }
.tree { list-style: none; margin: 0 0 14px; padding: 0; }
.tree li > .row {
  display: flex; align-items: center; gap: 6px; padding: 5px 8px; border-radius: 6px; cursor: pointer;
}
.tree li > .row:hover { background: var(--skipped); }
.tree li > .row:hover .icon-btn { visibility: visible; }
.tree li > .row.active { background: var(--accent-soft); color: var(--accent); }
.tree .label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree .sub { font-size: 12px; color: var(--muted); }
.tree ul { list-style: none; padding-left: 16px; margin: 0; }
.tree .empty { color: var(--muted); font-size: 13px; padding: 4px 8px; }

main { flex: 1; min-width: 0; display: flex; flex-direction: column; padding: 18px 22px; overflow: auto; }
main > .grid-wrap { min-height: 260px; }
.view-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 4px; }
.view-head h1 { font-size: 20px; margin: 0; }
.meta { color: var(--muted); margin-bottom: 14px; }
.hint { color: var(--muted); margin: 0 0 12px; }

.toolbar {
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  padding: 10px 12px; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; margin-bottom: 12px;
}
.toolbar input[type=number] { width: 80px; padding: 4px 6px; border: 1px solid var(--border); border-radius: 5px; background: var(--bg); }
.toolbar .spacer { flex: 1; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; max-height: 96px; overflow-y: auto; }
.chip {
  border: 1px solid var(--border); background: var(--panel); border-radius: 999px; padding: 3px 10px; font-size: 12.5px;
}
.chip .type { color: var(--muted); margin-left: 6px; font-size: 11px; }

.grid-wrap { flex: 1; min-height: 0; overflow: auto; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; }
table.grid { border-collapse: separate; border-spacing: 0; font-size: 13px; white-space: nowrap; }
.grid th, .grid td { padding: 5px 10px; border-bottom: 1px solid var(--border); border-right: 1px solid var(--border); max-width: 340px; overflow: hidden; text-overflow: ellipsis; }
.grid thead th { position: sticky; top: 0; background: var(--grid-head); text-align: left; z-index: 1; font-weight: 600; }
.grid thead th .type { display: block; font-weight: 400; font-size: 11px; color: var(--muted); }
.grid td.rownum, .grid th.rownum { position: sticky; left: 0; background: var(--grid-head); color: var(--muted); text-align: right; font-size: 12px; }
.grid thead th.rownum { z-index: 2; }
.grid td.num { text-align: right; font-variant-numeric: tabular-nums; }
.grid td.null { color: var(--muted); font-style: italic; }
.grid.pick tbody tr { cursor: pointer; }
.grid.pick tbody tr:hover td { background: var(--accent-soft); }
.grid.pick tr.header-row td { background: var(--header-row); font-weight: 600; }
.grid.pick tr.skipped td { background: var(--skipped); color: var(--muted); text-decoration: line-through; }

.pager { display: flex; align-items: center; gap: 10px; padding-top: 10px; color: var(--muted); }

.welcome { margin: auto; text-align: center; max-width: 460px; color: var(--muted); }
.welcome h1 { color: var(--text); font-size: 22px; }
.welcome .drop-target {
  margin-top: 18px; border: 2px dashed var(--border); border-radius: 12px; padding: 36px; cursor: pointer;
}
.welcome .drop-target:hover { border-color: var(--accent); color: var(--accent); }

.drop-overlay {
  position: fixed; inset: 0; background: rgba(31, 111, 235, .12); border: 3px dashed var(--accent);
  display: flex; align-items: center; justify-content: center; font-size: 22px; color: var(--accent); z-index: 50;
}
.busy { position: fixed; inset: 0; background: rgba(0,0,0,.25); display: flex; align-items: center; justify-content: center; z-index: 60; }
.busy-card { background: var(--panel); padding: 22px 30px; border-radius: 10px; box-shadow: var(--shadow); min-width: 280px; text-align: center; }
.spinner { width: 28px; height: 28px; margin: 0 auto 12px; border: 3px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.progress { margin-top: 12px; height: 6px; background: var(--border); border-radius: 3px; overflow: hidden; }
#progress-bar { height: 100%; width: 0; background: var(--accent); transition: width .15s; }
/* ---------- derived columns & applied steps */
.grid thead th.derived { background: var(--new-col); cursor: pointer; }
.grid td.derived { background: color-mix(in srgb, var(--new-col) 45%, transparent); }
.chip.derived { background: var(--new-col); border-color: transparent; }

/* ---------- table area + applied steps panel */
.toolbar.slim { padding: 6px 10px; gap: 10px; margin-bottom: 10px; }
.viewing { color: #b7791f; font-size: 13px; display: inline-flex; align-items: center; gap: 8px; }
.btn.small { padding: 2px 9px; font-size: 12px; }
.table-area { flex: 1; min-height: 300px; display: flex; gap: 12px; }
.table-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.table-main .grid-wrap { flex: 1; min-height: 240px; }
.grid thead th.col-head { cursor: pointer; }
.grid thead th.col-head:hover { color: var(--accent); }
.grid .caret { margin-left: 6px; font-size: 10px; color: var(--muted); }
.ap-panel { width: 290px; flex-shrink: 0; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; display: flex; flex-direction: column; min-height: 0; }
.ap-head { display: flex; justify-content: space-between; align-items: center; padding: 10px 12px; border-bottom: 1px solid var(--border);
  font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 600; }
.ap-list { list-style: none; margin: 0; padding: 6px; overflow-y: auto; flex: 1; counter-reset: step -1; }
.ap-step { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 6px; cursor: pointer; font-size: 13px; }
.ap-step:hover { background: var(--skipped); }
.ap-step.selected { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.ap-step.after { opacity: .5; }
.ap-icon { width: 22px; text-align: center; flex-shrink: 0; color: var(--muted); font-size: 12px; }
.ap-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.step-tools { display: none; gap: 0; }
.ap-step:hover .step-tools, .ap-step.selected .step-tools { display: inline-flex; }
.step-tools .icon-btn { visibility: visible; padding: 1px 4px; }
.step-tools .icon-btn:hover { color: var(--accent); }
.icon-btn.danger:hover { color: var(--danger) !important; }
.icon-btn:disabled { opacity: .3; cursor: default; }
.ap-foot { padding: 8px 12px; border-top: 1px solid var(--border); }

.spacer { flex: 1; }
.topbar { gap: 10px; }

/* ---------- storage */
.sidebar-foot { border-top: 1px solid var(--border); padding-top: 10px; }
.sidebar-foot .link-btn { border: none; background: none; padding: 4px 6px; font-size: 13px; }
.storage-row { display: grid; grid-template-columns: 1fr 90px 90px; gap: 12px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--border); }
.storage-size { text-align: right; font-variant-numeric: tabular-nums; }
.disk-bar { height: 8px; background: var(--border); border-radius: 4px; overflow: hidden; }
.disk-bar > div { height: 100%; background: var(--accent); }

/* ---------- recipes */
.sidebar h2.with-action { display: flex; justify-content: space-between; align-items: center; }
.link-btn { cursor: pointer; color: var(--accent); text-transform: none; letter-spacing: 0; font-weight: 400; font-size: 12px; }
.link-btn:hover { text-decoration: underline; }
.recipe-layout { display: grid; grid-template-columns: minmax(420px, 1.2fr) 1fr; gap: 16px; align-items: start; overflow: auto; }
.recipe-layout h2 { font-size: 15px; margin: 0 0 6px; }
.recipe-run, .recipe-what { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 14px;
  display: flex; flex-direction: column; gap: 10px; }
.recipe-card { border: 1px solid var(--border); border-radius: 8px; padding: 10px; display: flex; flex-direction: column; gap: 8px; background: var(--bg); }
.recipe-card-head { display: flex; flex-direction: column; gap: 2px; }
.recipe-src { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.recipe-src select { flex: 1; min-width: 240px; }
.fx-status.warn-box { border-color: #d69e2e; color: var(--text); }
.map-list { display: flex; flex-direction: column; gap: 4px; }
.map-row { display: grid; grid-template-columns: 1fr auto 1fr; gap: 8px; align-items: center; }
.map-name { font-weight: 600; }
.recipe-table { padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.recipe-table ol { margin: 6px 0 0; padding-left: 22px; color: var(--muted); }
.badge { margin-left: 8px; font-size: 11px; padding: 1px 7px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); }
.progress .bar { height: 100%; width: 0; background: var(--accent); transition: width .3s; }
.export-actions { display: flex; gap: 10px; }
a.btn { text-decoration: none; }
.grid td.diff { background: rgba(229, 72, 77, .18); color: var(--danger); font-weight: 600; }
.grid td.status-matched { color: #2f9e5b; }
.grid td.status-values { background: rgba(229, 72, 77, .14); color: var(--danger); font-weight: 600; }
.grid td.status-only { background: rgba(214, 158, 46, .16); color: #b7791f; font-weight: 600; }
.ap-head .btn.small { text-transform: none; letter-spacing: 0; font-weight: 400; }
.ap-parent { padding: 6px 12px; border-bottom: 1px solid var(--border); }
.ap-error { padding: 6px 12px; color: var(--danger); font-size: 12.5px; border-bottom: 1px solid var(--border); }
.new-table-opt { display: inline-flex; align-items: center; gap: 8px; }
.new-table-opt .input[hidden] { display: none; }
.dialog.wide { width: min(980px, 100%); }

/* ---------- drag & drop field areas (choose columns, pivot) */
.fz { display: grid; grid-template-columns: minmax(220px, 1fr) 1.3fr; gap: 12px; min-height: 320px; }
.fz-fields, .fz-area { display: flex; flex-direction: column; gap: 6px; border: 1px solid var(--border); border-radius: 8px; padding: 8px; background: var(--bg); min-height: 0; }
.fz-areas { display: flex; flex-direction: column; gap: 10px; }
.fz:not(.single) .fz-areas { display: grid; grid-template-rows: auto auto 1fr; }
.fz-title { font-weight: 600; font-size: 12.5px; }
.fz-list { display: flex; flex-direction: column; gap: 3px; overflow-y: auto; min-height: 60px; }
.fz-source { max-height: 380px; }
.fz.single .fz-zone { max-height: 380px; min-height: 280px; }
.fz-zone { min-height: 70px; max-height: 200px; }
.fz-area.over { border-color: var(--accent); background: var(--accent-soft); }
.fz-item, .fz-chip { display: flex; align-items: center; gap: 6px; padding: 4px 6px; border-radius: 5px; background: var(--panel);
  border: 1px solid var(--border); font-size: 13px; cursor: grab; user-select: none; }
.fz-item:hover, .fz-chip:hover { border-color: var(--accent); }
.fz-item.used { opacity: .6; }
.fz-item .hint { margin-left: auto; }
.fz-chip { background: var(--accent-soft); }
.fz-grip { color: var(--muted); font-size: 11px; letter-spacing: -2px; }
.fz-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fz-agg { padding: 2px 4px; font-size: 12px; max-width: 150px; }
.fz-empty { padding: 10px; text-align: center; border: 1px dashed var(--border); border-radius: 5px; }
.fz-actions { display: flex; gap: 8px; align-items: center; margin-top: 8px; }

/* ---------- menus */
.menu { list-style: none; margin: 0; padding: 4px; background: var(--panel); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: var(--shadow); min-width: 240px; font-size: 13px; }
.menu.root { position: fixed; z-index: 80; }
.menu li { display: flex; align-items: center; gap: 8px; padding: 6px 10px; border-radius: 5px; cursor: pointer; position: relative; white-space: nowrap; }
.menu li:hover { background: var(--accent-soft); color: var(--accent); }
.menu li.sep { height: 1px; padding: 0; margin: 4px 6px; background: var(--border); cursor: default; }
.mi-icon { width: 20px; text-align: center; color: var(--muted); }
.mi-arrow { margin-left: auto; color: var(--muted); }
.menu li > .menu { display: none; position: absolute; left: 100%; top: -4px; color: var(--text); }
.menu li.has-sub:hover > .menu { display: block; }

/* ---------- step form dialog */
.modal-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.3); z-index: 45; display: flex; align-items: center; justify-content: center; padding: 20px; }
.dialog { width: min(680px, 100%); max-height: 88vh; background: var(--panel); border-radius: 10px; box-shadow: var(--shadow); display: flex; flex-direction: column; }
.form-body { padding: 14px 18px; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; }
.form-body > .hint { margin: 0; font-size: 13px; }
.form-field { display: flex; flex-direction: column; gap: 5px; }
.form-label { font-weight: 600; font-size: 13px; }
.form-check { display: flex; align-items: center; gap: 6px; font-size: 13px; }
.check-box { display: flex; flex-direction: column; gap: 6px; border: 1px solid var(--border); border-radius: 6px; padding: 8px; }
.check-tools { display: flex; gap: 6px; }
.check-tools .input { flex: 1; }
.check-list { max-height: 190px; overflow-y: auto; display: flex; flex-direction: column; }
.check-list.tall { max-height: 360px; border: 1px solid var(--border); border-radius: 6px; padding: 4px; }
.check-item { display: flex; align-items: center; gap: 8px; padding: 3px 6px; border-radius: 4px; font-size: 13px; cursor: pointer; }
.check-item:hover { background: var(--skipped); }
.check-item > span:first-of-type { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.repeat { display: flex; flex-direction: column; gap: 6px; }
.repeat-row { display: flex; gap: 6px; align-items: flex-start; flex-wrap: wrap; padding: 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); }
.repeat-row > .form-field { flex: 1 1 160px; min-width: 0; }
.repeat-row > .form-field:has(.check-box) { flex-basis: 100%; }
.repeat .btn.small { align-self: flex-start; }
.rename-list { display: flex; flex-direction: column; gap: 4px; max-height: 420px; overflow-y: auto; }
.rename-row { display: grid; grid-template-columns: 1fr auto 1fr; gap: 8px; align-items: center; font-size: 13px; }
.rename-from { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- formula drawer */
.drawer-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.25); z-index: 40; display: flex; justify-content: flex-end; }
.drawer {
  width: min(620px, 100vw); height: 100%; background: var(--panel); box-shadow: var(--shadow);
  display: flex; flex-direction: column;
}
.drawer-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.drawer-head h2 { margin: 0; font-size: 17px; }
.drawer-body { flex: 1; overflow-y: auto; padding: 14px 18px; display: flex; flex-direction: column; gap: 12px; }
.drawer-foot { display: flex; align-items: center; justify-content: flex-end; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--border); }
.drawer-foot .hint { margin-right: auto; font-size: 12px; }
.icon-btn.visible { visibility: visible; font-size: 16px; }
.field { display: flex; flex-direction: column; gap: 5px; font-weight: 600; font-size: 13px; }
.input { padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); font-weight: 400; min-width: 0; }
.input:focus, .formula-input:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.formula-wrap { position: relative; }
.formula-input {
  width: 100%; resize: vertical; font-family: Consolas, "Cascadia Mono", monospace; font-size: 14px; font-weight: 400;
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg);
}
.suggest {
  position: absolute; left: 0; right: 0; top: 100%; z-index: 5; margin: 2px 0 0; padding: 4px; list-style: none;
  background: var(--panel); border: 1px solid var(--border); border-radius: 6px; box-shadow: var(--shadow); max-height: 260px; overflow-y: auto;
}
.suggest li { display: flex; justify-content: space-between; gap: 12px; padding: 5px 8px; border-radius: 4px; cursor: pointer; font-weight: 400; font-family: Consolas, monospace; font-size: 13px; }
.suggest li.active, .suggest li:hover { background: var(--accent-soft); color: var(--accent); }
.hint { color: var(--muted); font-weight: 400; font-size: 12px; }
.suggest .hint { font-family: "Segoe UI", sans-serif; }
.op-bar { display: flex; flex-wrap: wrap; gap: 4px; }
.op { min-width: 34px; padding: 3px 8px; border: 1px solid var(--border); background: var(--bg); border-radius: 5px; cursor: pointer; font-family: Consolas, monospace; font-weight: 400; }
.op:hover { border-color: var(--accent); color: var(--accent); }
.fx-status { padding: 10px 12px; border-radius: 6px; background: var(--bg); border: 1px solid var(--border); color: var(--muted); display: flex; flex-direction: column; gap: 4px; }
.fx-status.ok { border-color: #3a9d5d; }
.fx-status.error { border-color: var(--danger); color: var(--danger); }
.fx-status code { font-family: Consolas, monospace; background: var(--panel); padding: 1px 5px; border-radius: 4px; color: var(--text); }
.fx-status .warn { color: #b7791f; }
.fx-preview-title { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.grid-wrap.small { max-height: 260px; flex: none; }
.grid td.result { font-weight: 600; }
.grid th.derived { background: var(--new-col); }
.helpers { border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.tabs { display: flex; border-bottom: 1px solid var(--border); background: var(--grid-head); }
.tab { flex: 1; padding: 8px; border: none; background: none; cursor: pointer; color: var(--muted); }
.tab.active { color: var(--accent); background: var(--panel); font-weight: 600; }
.tab-body { padding: 10px; display: flex; flex-direction: column; gap: 8px; }
.pick-list { display: flex; flex-direction: column; max-height: 230px; overflow-y: auto; }
.pick-item { display: flex; justify-content: space-between; gap: 12px; text-align: left; padding: 6px 8px; border: none; background: none; border-radius: 5px; cursor: pointer; }
.pick-item:hover { background: var(--accent-soft); }
.pick-item .hint { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 65%; }
.cat-bar { display: flex; flex-wrap: wrap; gap: 4px; }
.cat { padding: 2px 10px; border-radius: 999px; border: 1px solid var(--border); background: var(--bg); cursor: pointer; font-size: 12px; }
.cat.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.builder:empty { display: none; }
.builder { border: 1px solid var(--accent); border-radius: 8px; padding: 10px; display: flex; flex-direction: column; gap: 8px; background: var(--accent-soft); }
.builder-head b { font-family: Consolas, monospace; }
.builder .hint { margin: 0; }
.arg-row { display: flex; flex-direction: column; gap: 3px; }
.arg-row label { font-size: 12px; font-weight: 600; }
.arg-inputs { display: flex; gap: 6px; align-items: center; }
.arg-inputs select { flex: 1; }
.arg-inputs input { flex: 1.2; }
.arg-inputs .op-sel { flex: 0 0 64px; }
.arg-inputs .or { color: var(--muted); font-size: 12px; }
.builder-actions { display: flex; justify-content: flex-end; gap: 6px; }

.toast {
  position: fixed; bottom: 20px; right: 20px; max-width: 460px; padding: 12px 16px; border-radius: 8px;
  background: var(--panel); border: 1px solid var(--border); box-shadow: var(--shadow); z-index: 70; white-space: pre-line;
}
.toast.error { border-color: var(--danger); color: var(--danger); }
