:root {
  color-scheme: dark;
  --bg: #050907;
  --surface: #09110e;
  --surface-2: #0d1713;
  --surface-3: #111d18;
  --line: rgba(148, 174, 164, 0.16);
  --line-strong: rgba(148, 174, 164, 0.3);
  --text: #edf5f1;
  --muted: #9aaba4;
  --faint: #667a72;
  --teal: #35e6d0;
  --teal-dark: #119a89;
  --green: #48df92;
  --amber: #f5b544;
  --red: #ff647c;
  --blue: #78a6ff;
  --mono: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; scroll-padding-top: 124px; }
html, body { margin: 0; min-height: 100%; }
body {
  overflow-x: hidden;
  color: var(--text);
  background-color: var(--bg);
  background-image:
    linear-gradient(rgba(108, 143, 131, 0.035) 1px, transparent 1px),
    linear-gradient(90deg, rgba(108, 143, 131, 0.035) 1px, transparent 1px);
  background-size: 42px 42px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button, input, select, textarea { font: inherit; }
button, summary, a { -webkit-tap-highlight-color: transparent; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  min-height: 58px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 20px;
  padding: 9px 18px;
  border-bottom: 1px solid var(--line);
  background: rgba(5, 9, 7, 0.95);
  backdrop-filter: blur(16px);
}
.brand { min-width: 0; display: flex; align-items: center; gap: 9px; color: var(--text); text-decoration: none; }
.brand img { width: 27px; height: 27px; }
.brand span { font-size: 13px; font-weight: 800; }
.brand small { padding: 3px 5px; border: 1px solid rgba(53, 230, 208, 0.28); border-radius: 4px; color: var(--teal); font: 750 7px var(--mono); }
.save-state { display: flex; align-items: center; gap: 7px; color: var(--muted); font-size: 11px; }
.save-state span { width: 7px; height: 7px; border-radius: 50%; background: var(--green); box-shadow: 0 0 0 4px rgba(72, 223, 146, 0.08); }
.topbar__actions { justify-self: end; display: flex; align-items: center; gap: 15px; }
.topbar__actions > a { color: var(--muted); text-decoration: none; font-size: 12px; font-weight: 650; }
.topbar__actions > a:hover { color: var(--text); }
.language { display: flex; padding: 2px; border: 1px solid var(--line); border-radius: 6px; }
.language button { min-width: 34px; height: 28px; border: 0; border-radius: 4px; color: var(--faint); background: transparent; cursor: pointer; font-size: 11px; font-weight: 750; }
.language button.is-active { color: #03110e; background: var(--teal); }

.builder-shell { width: min(1180px, calc(100% - 40px)); margin: 0 auto; padding: 42px 0 80px; }
.builder-intro { display: flex; align-items: end; justify-content: space-between; gap: 36px; padding-bottom: 28px; }
.builder-intro > div:first-child { max-width: 720px; }
.builder-intro p { margin: 0 0 8px; color: var(--teal); font: 750 11px var(--mono); }
.builder-intro h1 { margin: 0; font-size: clamp(28px, 4vw, 46px); line-height: 1.08; letter-spacing: 0; }
.builder-intro > div:first-child > span { display: block; margin-top: 12px; color: var(--muted); font-size: 15px; line-height: 1.55; }

.identity-stats { width: 330px; display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.identity-stats div { min-width: 0; padding: 10px; border-left: 2px solid var(--line-strong); }
.identity-stats strong, .identity-stats span { display: block; }
.identity-stats strong { color: var(--text); font: 750 19px var(--mono); }
.identity-stats span { margin-top: 3px; color: var(--faint); font-size: 9px; line-height: 1.25; }

.step-nav {
  position: sticky;
  top: 58px;
  z-index: 30;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(9, 17, 14, 0.96);
  backdrop-filter: blur(14px);
  overflow: hidden;
}
.step-nav a { min-width: 0; min-height: 64px; display: grid; grid-template-columns: 32px 1fr; grid-template-rows: auto auto; align-content: center; column-gap: 10px; padding: 10px 16px; border-right: 1px solid var(--line); color: var(--text); text-decoration: none; }
.step-nav a:last-child { border-right: 0; }
.step-nav a:hover { background: rgba(53, 230, 208, 0.045); }
.step-nav a > span { grid-row: 1 / 3; width: 29px; height: 29px; display: grid; place-items: center; align-self: center; border: 1px solid var(--line-strong); border-radius: 50%; color: var(--teal); font: 750 11px var(--mono); }
.step-nav strong { overflow: hidden; font-size: 12px; text-overflow: ellipsis; white-space: nowrap; }
.step-nav small { margin-top: 2px; color: var(--faint); font-size: 9px; }
.step-nav small[data-state="complete"] { color: var(--green); }
.step-nav small[data-state="issues"] { color: var(--amber); }

.builder-workspace { display: grid; gap: 18px; margin-top: 20px; }
.workflow-step { scroll-margin-top: 138px; border: 1px solid var(--line); border-radius: 8px; background: rgba(9, 17, 14, 0.97); overflow: hidden; }
.step-head { min-height: 92px; display: grid; grid-template-columns: 42px 1fr; align-items: center; gap: 10px; padding: 20px 24px; border-bottom: 1px solid var(--line); }
.step-head > span { width: 38px; height: 38px; display: grid; place-items: center; border: 1px solid rgba(53, 230, 208, 0.32); border-radius: 50%; color: var(--teal); font: 750 11px var(--mono); }
.step-head h2 { margin: 0; font-size: 19px; }
.step-head p { margin: 4px 0 0; color: var(--muted); font-size: 12px; line-height: 1.4; }
.step-head--action { grid-template-columns: 42px 1fr auto; }

label { display: block; }
label > span, .control-label { display: block; margin-bottom: 6px; color: var(--muted); font-size: 11px; font-weight: 750; }
label > small { display: block; margin-top: 5px; color: var(--faint); font-size: 9px; line-height: 1.35; }
input, select, textarea { width: 100%; border: 1px solid var(--line-strong); border-radius: 6px; color: var(--text); background: #07100d; outline: none; }
input, select { height: 42px; padding: 0 11px; font-size: 12px; }
textarea { padding: 10px 11px; resize: vertical; font-size: 12px; line-height: 1.5; }
input::placeholder, textarea::placeholder { color: #4f625a; }
input:focus, select:focus, textarea:focus { border-color: var(--teal-dark); box-shadow: 0 0 0 3px rgba(53, 230, 208, 0.07); }
.code-input, pre { font-family: var(--mono); }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-grid--id { grid-template-columns: minmax(0, 1.5fr) minmax(110px, 0.5fr); }

.starter-block { padding: 20px 24px; border-bottom: 1px solid var(--line); background: rgba(53, 230, 208, 0.025); }
.starter-block > p { margin: 7px 0 0; color: var(--faint); font-size: 10px; }
.load-row { display: grid; grid-template-columns: minmax(0, 1fr) 150px; gap: 8px; }
.load-button { min-height: 42px; }
.essential-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 18px 20px; padding: 24px; }
.essential-fields label:nth-child(2) { grid-row: span 2; }

.advanced-block { border-top: 1px solid var(--line); background: rgba(5, 9, 7, 0.36); }
.advanced-block summary { min-height: 48px; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 0 24px; cursor: pointer; list-style: none; }
.advanced-block summary::-webkit-details-marker { display: none; }
.advanced-block summary span { font-size: 11px; font-weight: 750; }
.advanced-block summary span::before { content: "+"; display: inline-block; width: 18px; color: var(--teal); font: 750 13px var(--mono); }
.advanced-block[open] summary span::before { content: "−"; }
.advanced-block summary small { color: var(--faint); font-size: 9px; }
.advanced-content { display: grid; gap: 13px; padding: 0 24px 22px; }
.risk-config { padding: 12px; border: 1px solid var(--line); border-radius: 6px; background: #08100d; }
.range-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.range-head label { margin: 0; color: var(--muted); font-size: 10px; font-weight: 750; }
.range-head output { color: var(--teal); font: 750 11px var(--mono); }
input[type="range"] { height: 20px; padding: 0; border: 0; accent-color: var(--teal); }

.plain-guide { margin: 20px 24px 0; padding: 14px 16px; border-left: 3px solid var(--blue); background: rgba(120, 166, 255, 0.055); }
.plain-guide strong { font-size: 11px; }
.plain-guide p { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; margin: 6px 0 0; color: var(--muted); font-size: 11px; }
.plain-guide b { color: var(--teal); }
.primary-button, .secondary-button { min-height: 38px; padding: 0 14px; border-radius: 6px; cursor: pointer; font-size: 11px; font-weight: 800; }
.primary-button { border: 0; color: #03110e; background: var(--teal); }
.primary-button > span:first-child { margin-right: 5px; font-size: 15px; }
.secondary-button { border: 1px solid var(--line-strong); color: var(--text); background: transparent; }
.primary-button:hover { background: #72f1e1; }
.secondary-button:hover { border-color: var(--teal-dark); }
.primary-button:disabled, .secondary-button:disabled { color: var(--faint); border: 1px solid var(--line); background: transparent; cursor: not-allowed; }

.rule-tabs { min-height: 54px; display: flex; align-items: center; gap: 7px; overflow-x: auto; padding: 10px 24px; border-bottom: 1px solid var(--line); }
.rule-tab { min-width: 128px; height: 34px; display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 0 10px; border: 1px solid var(--line); border-radius: 6px; color: var(--muted); background: var(--surface); cursor: pointer; }
.rule-tab span { overflow: hidden; font: 700 10px var(--mono); text-overflow: ellipsis; white-space: nowrap; }
.rule-tab small { width: 7px; height: 7px; flex: 0 0 auto; border-radius: 50%; background: var(--faint); }
.rule-tab.is-active { color: var(--text); border-color: rgba(53, 230, 208, 0.48); background: rgba(53, 230, 208, 0.07); }
.rule-tab.is-active small { background: var(--teal); box-shadow: 0 0 0 4px rgba(53, 230, 208, 0.08); }

.rule-editor { padding: 24px; }
.rule-shell { width: min(900px, 100%); margin: 0 auto; }
.rule-summary { margin-bottom: 14px; padding: 15px 16px; border: 1px solid rgba(53, 230, 208, 0.25); border-radius: 7px; background: rgba(53, 230, 208, 0.045); }
.rule-summary span, .rule-summary strong { display: block; }
.rule-summary span { color: var(--teal); font: 750 9px var(--mono); }
.rule-summary strong { margin-top: 6px; font-size: 13px; line-height: 1.5; }
.rule-decision { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.rule-reasons { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 15px; }
.rule-advanced { margin-top: 15px; border: 1px solid var(--line); border-radius: 7px; }
.rule-advanced .advanced-content { grid-template-columns: minmax(170px, 1fr) 140px auto; align-items: end; }
.rule-advanced .delete-rule { align-self: end; }
.delete-rule { min-width: 42px; height: 42px; padding: 0 12px; border: 1px solid rgba(255, 100, 124, 0.3); border-radius: 6px; color: var(--red); background: transparent; cursor: pointer; font-size: 11px; font-weight: 750; }

.condition-section { position: relative; margin-top: 18px; padding: 16px; border: 1px solid var(--line); border-radius: 7px; background: rgba(7, 15, 12, 0.96); overflow: hidden; }
.condition-section::before { content: ""; position: absolute; inset: 0; pointer-events: none; background: linear-gradient(90deg, transparent, rgba(53, 230, 208, 0.025), transparent); transform: translateX(-100%); animation: scan 8s linear infinite; }
.condition-section__head { position: relative; display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.condition-section__head div:first-child span, .condition-section__head div:first-child strong, .condition-section__head div:first-child small { display: block; }
.condition-section__head span { color: var(--teal); font: 750 9px var(--mono); }
.condition-section__head strong { margin-top: 3px; font-size: 13px; }
.condition-section__head small { margin-top: 3px; color: var(--faint); font-size: 9px; }
.condition-actions, .tree-actions { display: flex; gap: 6px; }
.condition-actions button, .tree-actions button { min-height: 31px; padding: 0 9px; border: 1px solid var(--line-strong); border-radius: 5px; color: var(--muted); background: #0b1511; cursor: pointer; font-size: 9px; font-weight: 750; }
.condition-actions button:hover, .tree-actions button:hover { color: var(--teal); border-color: rgba(53, 230, 208, 0.38); }
.condition-tree { position: relative; }
.condition-group { position: relative; margin: 10px 0 0 14px; padding: 10px 10px 10px 20px; border-left: 1px solid rgba(53, 230, 208, 0.28); }
.condition-group::before { content: ""; position: absolute; top: 0; left: -4px; width: 7px; height: 7px; border: 1px solid var(--teal-dark); border-radius: 50%; background: #07100d; }
.condition-group[data-depth="0"] { margin-left: 0; padding-left: 0; border-left: 0; }
.condition-group[data-depth="0"]::before { display: none; }
.group-head { min-height: 36px; display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.group-mode { width: auto; min-width: 220px; height: 34px; color: var(--teal); font-size: 10px; font-weight: 750; }
.tree-actions .remove-node { width: 31px; padding: 0; color: var(--red); }
.condition-leaf { position: relative; display: grid; grid-template-columns: minmax(150px, 1.1fr) minmax(145px, 0.9fr) minmax(170px, 1.25fr) 31px; gap: 8px; margin: 9px 0 0 14px; padding: 12px 0 0 16px; border-top: 1px solid rgba(148, 174, 164, 0.08); }
.condition-leaf::before { content: ""; position: absolute; top: 32px; left: 0; width: 10px; height: 1px; background: var(--line-strong); }
.condition-leaf label { min-width: 0; }
.condition-leaf label span { margin-bottom: 4px; color: var(--faint); font-size: 8px; text-transform: uppercase; }
.condition-leaf input, .condition-leaf select { height: 38px; }
.condition-leaf .custom-field { margin-top: 6px; border-style: dashed; font-family: var(--mono); font-size: 10px; }
.condition-leaf .remove-node { align-self: end; width: 31px; height: 38px; padding: 0; border: 1px solid rgba(255, 100, 124, 0.26); border-radius: 5px; color: var(--red); background: transparent; cursor: pointer; }
.tree-empty { margin: 9px 0 0 14px; padding: 12px; border: 1px dashed var(--line-strong); border-radius: 5px; color: var(--faint); font-size: 10px; }

.test-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(340px, 0.85fr); gap: 24px; padding: 24px; }
.test-inputs > label span { font-size: 12px; }
.test-inputs > label textarea { min-height: 116px; font-size: 14px; }
.test-advanced { margin-top: 14px; border: 1px solid var(--line); border-radius: 7px; }
.test-advanced summary { padding: 0 14px; }
.test-advanced .advanced-content { padding: 0 14px 14px; }
.context-editor { display: grid; gap: 9px; margin-top: 16px; padding-top: 15px; border-top: 1px solid var(--line); }
.context-editor__head { display: flex; align-items: center; justify-content: space-between; gap: 14px; }
.context-editor__head strong, .context-editor__head small { display: block; }
.context-editor__head strong { font-size: 12px; }
.context-editor__head small { margin-top: 3px; color: var(--faint); font-size: 9px; line-height: 1.35; }
.context-editor__head button { flex: 0 0 auto; }
.context-editor__head button span:first-child { margin-right: 4px; color: var(--teal); }
.context-fields { display: grid; gap: 7px; }
.context-row { display: grid; grid-template-columns: minmax(120px, 0.9fr) minmax(150px, 1.1fr) 38px; gap: 7px; align-items: end; }
.context-row label { min-width: 0; }
.context-row input { height: 38px; }
.context-row button { width: 38px; height: 38px; border: 1px solid rgba(255, 100, 124, 0.25); border-radius: 5px; color: var(--red); background: transparent; cursor: pointer; }
.context-row button:hover { border-color: rgba(255, 100, 124, 0.52); }
.context-json { border-top: 1px solid var(--line); padding-top: 10px; }
.context-json summary { cursor: pointer; color: var(--faint); font-size: 9px; font-weight: 700; }
.context-json label { margin-top: 10px; }
.test-results { display: grid; align-content: start; gap: 12px; }
.match-result, .validation { padding: 15px; border: 1px solid var(--line); border-radius: 7px; background: #08100d; }
.match-result > div:first-child, .validation > div:first-child { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.match-result span, .validation span { color: var(--faint); font: 750 9px var(--mono); }
.match-result strong, .validation strong { color: var(--muted); font: 800 10px var(--mono); }
.match-result p { margin: 10px 0 0; color: var(--muted); font-size: 11px; line-height: 1.5; }
.match-result[data-state="match"] { border-color: rgba(245, 181, 68, 0.46); }
.match-result[data-state="match"] strong { color: var(--amber); }
.match-result[data-state="miss"] strong { color: var(--green); }
.condition-results { display: grid; gap: 5px; margin-top: 11px; }
.condition-result { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding-top: 6px; border-top: 1px solid var(--line); color: var(--faint); font-size: 9px; }
.condition-result b { color: var(--red); }
.condition-result.is-match b { color: var(--green); }
.validation[data-state="invalid"] { border-color: rgba(255, 100, 124, 0.38); }
.validation[data-state="invalid"] strong { color: var(--red); }
.validation[data-state="valid"] strong { color: var(--green); }
.validation ul { margin: 9px 0 0; padding-left: 16px; color: var(--muted); font-size: 9px; line-height: 1.55; }
.validation ul:empty { display: none; }
.manifest-preview { margin: 0 24px; border-top: 1px solid var(--line); }
.manifest-preview summary { min-height: 48px; display: flex; align-items: center; justify-content: space-between; gap: 8px; cursor: pointer; list-style: none; }
.manifest-preview summary::-webkit-details-marker { display: none; }
.manifest-preview summary span { font-size: 11px; font-weight: 750; }
.manifest-preview summary small { color: var(--faint); font-size: 9px; }
.manifest-preview pre { max-height: 300px; margin: 0 0 18px; overflow: auto; padding: 12px; border: 1px solid var(--line); border-radius: 6px; color: #b9cbc4; background: #060d0a; font-size: 9px; line-height: 1.5; white-space: pre-wrap; word-break: break-word; }
.export-footer { display: flex; align-items: center; justify-content: space-between; gap: 18px; padding: 18px 24px 22px; }
.export-actions { display: flex; gap: 8px; }
.security-note { margin: 0; color: var(--faint); font-size: 9px; line-height: 1.45; }
.toast { position: fixed; right: 18px; bottom: 18px; z-index: 50; max-width: 320px; padding: 10px 13px; border: 1px solid var(--line-strong); border-radius: 6px; color: var(--text); background: #101a17; box-shadow: 0 14px 40px rgba(0, 0, 0, 0.35); font-size: 11px; opacity: 0; transform: translateY(8px); pointer-events: none; transition: opacity 0.2s ease, transform 0.2s ease; }
.toast.is-visible { opacity: 1; transform: translateY(0); }

@keyframes scan { to { transform: translateX(100%); } }

@media (max-width: 900px) {
  .topbar { grid-template-columns: 1fr auto; }
  .save-state { display: none; }
  .builder-shell { width: min(100% - 28px, 760px); padding-top: 28px; }
  .builder-intro { align-items: start; flex-direction: column; }
  .identity-stats { width: 100%; }
  .test-grid { grid-template-columns: 1fr; }
  .condition-leaf { grid-template-columns: 1fr 1fr 31px; }
  .condition-leaf label:nth-child(3) { grid-column: 1 / 3; }
}

@media (max-width: 640px) {
  html { scroll-padding-top: 108px; }
  .topbar { padding: 8px 12px; }
  .brand small, .topbar__actions > a { display: none; }
  .builder-shell { width: 100%; padding: 24px 0 60px; }
  .builder-intro { padding: 0 16px 22px; }
  .builder-intro h1 { font-size: 28px; }
  .step-nav { top: 54px; border-right: 0; border-left: 0; border-radius: 0; }
  .step-nav a { min-height: 54px; grid-template-columns: 26px 1fr; padding: 8px; }
  .step-nav a > span { width: 24px; height: 24px; }
  .step-nav strong { font-size: 9px; }
  .step-nav small { display: none; }
  .builder-workspace { gap: 12px; }
  .workflow-step { border-right: 0; border-left: 0; border-radius: 0; }
  .step-head, .step-head--action { grid-template-columns: 34px 1fr; padding: 16px; }
  .step-head > span { width: 31px; height: 31px; }
  .step-head--action .primary-button { grid-column: 2; justify-self: start; }
  .starter-block, .essential-fields, .rule-editor, .test-grid { padding: 16px; }
  .load-row { grid-template-columns: 1fr; }
  .essential-fields, .form-grid, .rule-decision, .rule-reasons { grid-template-columns: 1fr; }
  .essential-fields label:nth-child(2) { grid-row: auto; }
  .advanced-block summary, .advanced-content { padding-left: 16px; padding-right: 16px; }
  .plain-guide { margin: 16px 16px 0; }
  .rule-tabs { padding: 10px 16px; }
  .rule-advanced .advanced-content { grid-template-columns: 1fr; }
  .condition-section__head { align-items: flex-start; flex-direction: column; }
  .condition-actions { width: 100%; }
  .condition-actions button { flex: 1; }
  .group-head { align-items: flex-start; flex-direction: column; }
  .group-mode { width: 100%; min-width: 0; }
  .condition-leaf { grid-template-columns: 1fr 31px; margin-left: 0; padding-left: 0; }
  .condition-leaf::before { display: none; }
  .condition-leaf label, .condition-leaf label:nth-child(3) { grid-column: 1; }
  .condition-leaf .remove-node { grid-column: 2; grid-row: 1; }
  .context-editor__head { align-items: flex-start; }
  .context-row { grid-template-columns: 1fr 38px; }
  .context-row label { grid-column: 1; }
  .context-row label:nth-child(2) { grid-row: 2; }
  .context-row button { grid-column: 2; grid-row: 1; }
  .manifest-preview { margin: 0 16px; }
  .export-footer { align-items: stretch; flex-direction: column; padding: 16px; }
  .export-actions { display: grid; grid-template-columns: 1fr 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition-duration: 0.01ms !important; }
}
