* { margin: 0; padding: 0; box-sizing: border-box; }
:root {
  --bg: #0d1117; --surface: #161b22; --border: #30363d;
  --text: #e6edf3; --muted: #8b949e; --accent: #58a6ff;
  --accent-dim: #1f6feb33; --danger: #f85149; --success: #3fb950;
  --input-bg: #0d1117; --hover: #1c2128;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
}
body { background: var(--bg); color: var(--text); font-size: 14px; }

/* Header */
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: 100;
}
header h1 { font-size: 18px; font-weight: 600; }
.header-left { display: flex; align-items: center; gap: 10px; }
.menu-btn {
  display: none; background: none; border: 1px solid var(--border);
  color: var(--text); font-size: 20px; padding: 4px 10px; border-radius: 6px;
  cursor: pointer; line-height: 1;
}
.menu-btn:hover { background: var(--hover); }
.header-controls { display: flex; align-items: center; gap: 16px; }

/* Parameter search */
.search-container { position: relative; }
#param-search {
  background: var(--surface); color: var(--text); border: 1px solid var(--border);
  border-radius: 6px; padding: 5px 10px; font-size: 13px; width: 200px;
  outline: none;
}
#param-search:focus { border-color: var(--accent); }
#param-search::placeholder { color: var(--muted); }
.search-dropdown {
  position: absolute; top: 100%; left: 0; right: 0; min-width: 300px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px;
  margin-top: 4px; max-height: 320px; overflow-y: auto; z-index: 200;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
}
.search-dropdown.hidden { display: none; }
.search-result {
  padding: 8px 12px; cursor: pointer; display: flex; flex-direction: column; gap: 2px;
  border-bottom: 1px solid var(--border);
}
.search-result:last-child { border-bottom: none; }
.search-result:hover { background: var(--hover); }
.search-field { font-weight: 600; font-size: 13px; color: var(--text); }
.search-section { font-size: 11px; color: var(--accent); }
.search-hint { font-size: 11px; color: var(--muted); }
.search-empty { padding: 12px; text-align: center; color: var(--muted); font-size: 12px; }
.search-highlight {
  animation: search-pulse 2s ease-out;
}
@keyframes search-pulse {
  0% { background: rgba(88,166,255,0.2); }
  100% { background: transparent; }
}
.dim-select { display: flex; align-items: center; gap: 8px; }
.dim-select span { color: var(--muted); font-size: 13px; }
.dim-select select {
  background: var(--input-bg); color: var(--accent); border: 1px solid var(--border);
  border-radius: 6px; padding: 6px 12px; font-size: 14px; font-weight: 600;
  cursor: pointer;
}
.header-buttons { display: flex; gap: 8px; }
.header-buttons button, #btn-copy {
  background: var(--surface); color: var(--text); border: 1px solid var(--border);
  border-radius: 6px; padding: 6px 14px; font-size: 13px; cursor: pointer;
  transition: background 0.15s;
}
.header-buttons button:hover, #btn-copy:hover { background: var(--hover); }
.header-buttons button.primary {
  background: #238636; border-color: #2ea043; color: #fff; font-weight: 600;
}
.header-buttons button.primary:hover { background: #2ea043; }

/* Layout */
.layout {
  display: grid; grid-template-columns: 200px 1fr 5px var(--preview-width, 50vw);
  height: calc(100vh - 53px);
}

/* Resize handle */
.resize-handle {
  width: 5px; cursor: col-resize; background: var(--border);
  transition: background 0.15s;
  flex-shrink: 0; z-index: 10;
}
.resize-handle:hover, .resize-handle.dragging {
  background: var(--accent);
}

/* Sidebar */
#sidebar {
  background: var(--surface); border-right: 1px solid var(--border);
  overflow-y: auto; padding: 8px 0;
}
#nav-list { list-style: none; }
#nav-list li {
  padding: 7px 16px; cursor: pointer; font-size: 13px;
  color: var(--muted); border-left: 2px solid transparent;
  transition: all 0.15s;
}
#nav-list li:hover { color: var(--text); background: var(--hover); }
#nav-list li.active { color: var(--accent); border-left-color: var(--accent); background: var(--accent-dim); }
#nav-list li.section-header {
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em;
  color: var(--muted); cursor: default; padding-top: 14px; font-weight: 600;
}

/* Main */
#main {
  overflow-y: auto; padding: 20px 28px;
}
.section { display: none; }
.section.active { display: block; }
.section h2 {
  font-size: 16px; margin-bottom: 4px; display: flex; align-items: center; gap: 10px;
}
.section h2 .tag {
  font-size: 10px; padding: 2px 6px; border-radius: 10px;
  background: var(--accent-dim); color: var(--accent); font-weight: 600;
}
.section h2 .tag.optional { background: #3d2e0033; color: #d29922; }
.section-desc { color: var(--muted); font-size: 12px; margin-bottom: 16px; }

/* Species tabs */
.species-tabs {
  display: flex; gap: 4px; margin-bottom: 16px; flex-wrap: wrap;
}
.species-tabs button {
  background: var(--surface); color: var(--muted); border: 1px solid var(--border);
  border-radius: 6px; padding: 5px 14px; font-size: 12px; cursor: pointer;
}
.species-tabs button.active { color: var(--accent); border-color: var(--accent); background: var(--accent-dim); }
.species-tabs button.add { color: var(--success); border-style: dashed; }
.species-tabs button.remove { color: var(--danger); border-style: dashed; margin-left: auto; }

/* Injector tabs */
.injector-tabs {
  display: flex; gap: 4px; margin-bottom: 8px; flex-wrap: wrap;
}
.injector-tabs button {
  background: var(--surface); color: var(--muted); border: 1px solid var(--border);
  border-radius: 6px; padding: 4px 12px; font-size: 11px; cursor: pointer;
}
.injector-tabs button.active { color: var(--accent); border-color: var(--accent); background: var(--accent-dim); }
.injector-tabs button.add { color: var(--success); border-style: dashed; }
.injector-tabs button.remove { color: var(--danger); border-style: dashed; margin-left: auto; }

/* Validation messages */
.validation-msg { margin-bottom: 8px; }
.validation-warn {
  padding: 6px 12px; margin-bottom: 4px; border-radius: 6px;
  background: #d299221a; border: 1px solid #d2992233;
  color: #d29922; font-size: 12px;
}

/* Function parser help tooltip */
.fparser-help {
  display: inline-flex; align-items: center; justify-content: center;
  width: 15px; height: 15px; border-radius: 50%;
  background: var(--border); color: var(--muted);
  font-size: 10px; font-weight: bold; cursor: help;
  vertical-align: middle; margin-left: 4px;
  line-height: 1;
}
.fparser-help:hover { background: var(--accent); color: var(--bg); }

/* Field groups */
.field-group {
  margin-bottom: 20px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; overflow: hidden;
}
.field-group-title {
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em;
  padding: 8px 14px; color: var(--muted); background: var(--hover);
  border-bottom: 1px solid var(--border); font-weight: 600;
}
.field-row {
  display: grid; grid-template-columns: 200px 1fr; gap: 12px;
  padding: 8px 14px; align-items: start; border-bottom: 1px solid var(--border);
}
.field-row:last-child { border-bottom: none; }
.field-label {
  font-size: 13px; padding-top: 6px; display: flex; flex-direction: column; gap: 2px;
}
.field-label .name { font-weight: 500; color: var(--text); }
.field-label .hint { font-size: 11px; color: var(--muted); }
.field-input { display: flex; flex-direction: column; gap: 4px; }
.field-input input, .field-input select, .field-input textarea {
  background: var(--input-bg); color: var(--text); border: 1px solid var(--border);
  border-radius: 6px; padding: 6px 10px; font-size: 13px; font-family: inherit;
  width: 100%; transition: border-color 0.15s;
}
.field-input input:focus, .field-input select:focus, .field-input textarea:focus {
  outline: none; border-color: var(--accent);
}
.field-input textarea { resize: vertical; min-height: 32px; font-family: 'SF Mono', Menlo, monospace; }
.array-inputs { display: flex; gap: 6px; flex-wrap: wrap; }
.array-inputs input { width: 80px; text-align: center; }
.array-inputs .label { font-size: 11px; color: var(--muted); text-align: center; }
.array-col { display: flex; flex-direction: column; gap: 2px; align-items: center; }
.field-input .checkbox-row {
  display: flex; align-items: center; gap: 8px; padding: 4px 0;
}
.field-input .checkbox-row input[type=checkbox] {
  width: 16px; height: 16px; accent-color: var(--accent);
}
.dim-hidden { display: none !important; }

/* Phase space checkbox grid */
.phase-checkbox-grid {
  display: flex; flex-direction: column; gap: 8px;
}
.phase-group {
  display: flex; flex-direction: column; gap: 3px;
}
.phase-group-label {
  font-size: 11px; color: var(--muted); text-transform: uppercase;
  letter-spacing: 0.04em; font-weight: 600;
}
.phase-group-items {
  display: flex; flex-wrap: wrap; gap: 4px;
}
.phase-cb-label {
  display: inline-flex; align-items: center; gap: 4px;
  background: var(--input-bg); border: 1px solid var(--border);
  border-radius: 5px; padding: 4px 8px; cursor: pointer;
  font-size: 12px; font-family: 'SF Mono', Menlo, monospace;
  transition: border-color 0.15s, background 0.15s;
  user-select: none;
}
.phase-cb-label:hover {
  border-color: var(--accent); background: var(--hover);
}
.phase-cb-label:has(input:checked) {
  border-color: var(--accent); background: var(--accent-dim);
  color: var(--accent);
}
.phase-cb-label input[type=checkbox] {
  width: 13px; height: 13px; accent-color: var(--accent);
  margin: 0;
}

/* dt formula annotation */
.dt-formula {
  margin-bottom: 6px;
}
.dt-formula-content {
  display: flex; flex-direction: column; gap: 4px;
}
.dt-formula-label {
  font-size: 11px; color: var(--muted); font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.03em;
}
.dt-formula-inline {
  display: inline-flex; align-items: center; gap: 6px; flex-wrap: wrap;
}
.dt-formula-tex {
  font-size: 13px; color: var(--muted); line-height: 1.6;
  display: inline-flex; align-items: center;
}
.dt-formula-eq {
  font-size: 13px; color: var(--muted);
  display: inline-flex; align-items: center;
}

/* |B| magnitude panel */
#b-magnitude {
  margin-bottom: 6px;
  padding: 0 14px;
}

/* |B| field heatmap plot */
#b-field-plot {
  padding: 8px 14px 12px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
}
#b-field-canvas {
  border-radius: 4px;
  max-width: 100%;
  height: auto;
}
#b-field-controls {
  font-size: 12px; color: var(--muted);
}
#b-field-controls select {
  font-size: 12px; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 4px; padding: 2px 4px;
}
#b-field-plot-msg {
  font-size: 12px;
  color: var(--muted);
  font-style: italic;
}

/* |E| magnitude panel */
#e-magnitude {
  margin-bottom: 6px;
  padding: 0 14px;
}

/* |E| field heatmap plot */
#e-field-plot {
  padding: 8px 14px 12px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
}
#e-field-canvas {
  border-radius: 4px;
  max-width: 100%;
  height: auto;
}
#e-field-controls {
  font-size: 12px; color: var(--muted);
}
#e-field-controls select {
  font-size: 12px; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 4px; padding: 2px 4px;
}
#e-field-plot-msg {
  font-size: 12px;
  color: var(--muted);
  font-style: italic;
}

/* nsp density heatmap plot */
#nsp-plot {
  padding: 8px 14px 12px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
}
#nsp-canvas {
  border-radius: 4px;
  max-width: 100%;
  height: auto;
}
#nsp-plot-msg {
  font-size: 12px;
  color: var(--muted);
  font-style: italic;
}
#nsp-controls {
  font-size: 12px; color: var(--muted);
}
#nsp-controls select {
  font-size: 12px; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 4px; padding: 2px 4px;
}

/* vsp velocity plot */
#vsp-plot {
  padding: 8px 14px 12px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
}
#vsp-canvas {
  border-radius: 4px;
  max-width: 100%;
  height: auto;
}
#vsp-plot-msg {
  font-size: 12px;
  color: var(--muted);
  font-style: italic;
}
#vsp-controls {
  font-size: 12px; color: var(--muted);
}
#vsp-controls select {
  font-size: 12px; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 4px; padding: 2px 4px;
}

/* selectrule spatial plot */
#selectrule-plot {
  padding: 8px 14px 12px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
}
#selectrule-canvas {
  border-radius: 4px;
  max-width: 100%;
  height: auto;
}
#selectrule-plot-msg {
  font-size: 12px;
  color: var(--muted);
  font-style: italic;
}
#selectrule-controls {
  font-size: 12px; color: var(--muted);
}
#selectrule-controls select {
  font-size: 12px; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 4px; padding: 2px 4px;
}

/* 3D slice controls */
.plot-3d-controls {
  display: inline-flex; align-items: center; gap: 6px;
}
.plot-3d-controls select {
  font-size: 12px; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 4px; padding: 2px 4px;
}
.plot-3d-controls input[type="range"] {
  width: 120px; accent-color: var(--accent);
}
.plot-3d-controls span {
  font-family: 'SF Mono', Menlo, monospace; font-size: 11px;
  color: var(--muted); min-width: 36px;
}

/* Preview */
#preview {
  background: var(--surface); border-left: 1px solid var(--border);
  display: flex; flex-direction: column; overflow: hidden;
}
.preview-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px; border-bottom: 1px solid var(--border);
}
.preview-header h3 { font-size: 14px; font-weight: 600; }
#preview-text {
  flex: 1; overflow: auto; padding: 14px;
  font-family: 'SF Mono', Menlo, Consolas, monospace; font-size: 12px;
  line-height: 1.5; color: var(--muted); white-space: pre; tab-size: 4;
}

/* Modal */
.modal {
  position: fixed; inset: 0; background: #000a; display: flex;
  align-items: center; justify-content: center; z-index: 200;
}
.modal.hidden { display: none; }
.modal-content {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 20px; min-width: 320px; max-width: 500px;
}
.modal-content h3 { margin-bottom: 14px; }
.modal-close {
  margin-top: 14px; background: var(--hover); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px; padding: 6px 16px;
  cursor: pointer; font-size: 13px;
}
#preset-list button {
  display: block; width: 100%; text-align: left; background: none;
  color: var(--text); border: 1px solid var(--border); border-radius: 6px;
  padding: 10px 14px; margin-bottom: 8px; cursor: pointer; font-size: 13px;
  transition: background 0.15s;
}
#preset-list button:hover { background: var(--hover); }
#preset-list button .preset-desc { display: block; font-size: 11px; color: var(--muted); margin-top: 2px; }

/* Int enforcement */
input.int-field { font-variant-numeric: tabular-nums; }
input.int-field.rejected {
  animation: reject-flash 0.3s;
}
@keyframes reject-flash {
  0%   { border-color: var(--danger); box-shadow: 0 0 0 2px #f8514933; }
  100% { border-color: var(--border); box-shadow: none; }
}

/* Toast */
.toast {
  position: fixed; bottom: 20px; right: 20px; background: var(--success);
  color: #000; padding: 10px 18px; border-radius: 8px; font-size: 13px;
  font-weight: 600; z-index: 300; animation: fadeout 2s forwards;
}
@keyframes fadeout { 0%,70%{opacity:1} 100%{opacity:0} }

/* xres recommended values pills */
.xres-recommendations {
  margin-top: 6px; display: flex; flex-direction: column; gap: 4px;
}
.xres-rec-row {
  display: flex; align-items: center; gap: 4px; flex-wrap: wrap;
}
.xres-rec-row .rec-label {
  font-size: 11px; color: var(--muted); min-width: 14px; font-weight: 600;
}
.xres-rec-pill {
  background: var(--surface); color: var(--muted); border: 1px solid var(--border);
  border-radius: 12px; padding: 2px 10px; font-size: 11px; cursor: pointer;
  font-family: 'SF Mono', Menlo, monospace; transition: all 0.15s;
  white-space: nowrap; line-height: 1.4;
}
.xres-rec-pill:hover {
  border-color: var(--accent); color: var(--accent); background: var(--accent-dim);
}
.xres-rec-pill .pill-frac {
  font-size: 9px; color: var(--muted); margin-left: 2px;
}
.xres-rec-pill:hover .pill-frac { color: var(--accent); }

/* xres validation warning */
.xres-validation-msg { margin-top: 4px; }
.xres-validation-msg .validation-warn {
  padding: 4px 10px; margin-bottom: 2px; border-radius: 5px;
  background: #d299221a; border: 1px solid #d2992233;
  color: #d29922; font-size: 11px;
}

/* File size estimate panel */
.diag-size-panel {
  margin-top: 12px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; overflow: hidden;
}
.diag-size-panel .size-panel-header {
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em;
  padding: 8px 14px; color: var(--muted); background: var(--hover);
  border-bottom: 1px solid var(--border); font-weight: 600;
  display: flex; align-items: center; gap: 6px;
}
.diag-size-panel .size-panel-header .size-icon { font-size: 13px; }
.diag-size-panel .size-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 5px 14px; border-bottom: 1px solid var(--border);
  font-size: 12px;
}
.diag-size-panel .size-row:last-child { border-bottom: none; }
.diag-size-panel .size-row .size-name {
  font-family: 'SF Mono', Menlo, monospace; color: var(--text);
}
.diag-size-panel .size-row .size-dims {
  font-size: 11px; color: var(--muted); margin-left: 6px;
}
.diag-size-panel .size-row .size-value {
  font-family: 'SF Mono', Menlo, monospace; color: var(--accent); font-weight: 500;
}
.diag-size-panel .size-total {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 14px; background: var(--hover);
  border-top: 1px solid var(--border);
  font-size: 12px; font-weight: 600;
}
.diag-size-panel .size-total .size-value {
  font-family: 'SF Mono', Menlo, monospace; color: var(--accent);
}
.diag-size-panel .size-total.size-sim {
  border-top: 1px solid var(--border);
  background: var(--accent-dim);
}
.diag-size-panel .size-total.size-sim .size-value {
  color: var(--text);
}
.diag-size-panel .size-empty {
  padding: 12px 14px; text-align: center; color: var(--muted); font-size: 12px;
}

/* Node optimizer panel */
.node-optimizer-panel {
  margin-top: 12px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; overflow: hidden;
}
.node-optimizer-header {
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em;
  padding: 8px 14px; color: var(--muted); background: var(--hover);
  border-bottom: 1px solid var(--border); font-weight: 600;
}
.node-optimizer-desc {
  font-size: 11px; color: var(--muted); padding: 6px 14px 0;
  margin: 0; line-height: 1.5;
}
.node-optimizer-body {
  padding: 10px 14px;
}
.node-optimizer-input-row {
  display: flex; align-items: center; gap: 10px; margin-bottom: 10px;
}
.node-optimizer-input-row label {
  font-size: 12px; color: var(--muted); white-space: nowrap;
}
.node-optimizer-input-row input[type="range"] {
  flex: 1; max-width: 200px; accent-color: var(--accent);
}
.node-opt-target-label {
  font-family: 'SF Mono', Menlo, monospace; font-size: 12px;
  color: var(--text); min-width: 90px;
}
.node-opt-summary {
  display: flex; gap: 16px; flex-wrap: wrap; margin-bottom: 8px;
  font-size: 12px; color: var(--muted);
}
.node-opt-summary strong {
  color: var(--text); font-family: 'SF Mono', Menlo, monospace;
}
.node-opt-table {
  width: 100%; border-collapse: collapse; font-size: 12px;
}
.node-opt-table th {
  text-align: left; padding: 4px 8px; color: var(--muted); font-weight: 600;
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.03em;
  border-bottom: 1px solid var(--border);
}
.node-opt-table td {
  padding: 4px 8px; font-family: 'SF Mono', Menlo, monospace;
  border-bottom: 1px solid var(--border); color: var(--text); font-size: 12px;
}
.node-opt-table tr:last-child td { border-bottom: none; }
.node-opt-table tr.node-opt-recommended {
  background: var(--accent-dim);
}
.node-opt-table tr.node-opt-recommended td {
  color: var(--accent);
}
.node-opt-apply {
  background: var(--surface); color: var(--muted); border: 1px solid var(--border);
  border-radius: 10px; padding: 2px 10px; font-size: 11px; cursor: pointer;
  transition: all 0.15s; white-space: nowrap;
}
.node-opt-apply:hover {
  border-color: var(--accent); color: var(--accent); background: var(--accent-dim);
}
tr.node-opt-recommended .node-opt-apply {
  border-color: var(--accent); color: var(--accent);
}
.node-opt-empty {
  font-size: 12px; color: var(--muted); padding: 8px 0; font-style: italic;
}

/* Responsive */
@media (max-width: 1100px) {
  .layout { grid-template-columns: 180px 1fr; }
  #preview { display: none; }
  .resize-handle { display: none; }
}
@media (max-width: 768px) {
  header {
    flex-wrap: wrap; padding: 10px 12px; gap: 8px;
  }
  .menu-btn { display: inline-flex; }
  header h1 { font-size: 16px; }
  .header-controls {
    width: 100%; justify-content: flex-start; flex-wrap: wrap; gap: 8px;
  }
  .layout {
    display: flex; flex-direction: column;
    height: auto; min-height: calc(100vh - 90px);
  }
  .menu-btn { display: block; }
  #sidebar {
    display: none !important;
    border-right: none;
  }
  #sidebar.open {
    display: block !important; position: fixed; top: 0; left: 0; bottom: 0;
    width: 240px; z-index: 150; background: var(--surface);
    box-shadow: 4px 0 20px #0008; overflow-y: auto; padding: 8px 0;
  }
  .mobile-overlay {
    display: none; position: fixed; inset: 0; background: #0006; z-index: 140;
  }
  .mobile-overlay.open { display: block; }
  #main { padding: 14px 12px; overflow-x: hidden; flex: 1; }
  #preview { display: none !important; }
  .field-row {
    grid-template-columns: 1fr;
    gap: 4px;
  }
  .field-label { padding-top: 0; }
  .field-input { width: 100%; }
  .array-inputs { flex-wrap: wrap; gap: 8px; }
  .array-inputs input { width: 70px !important; }
  .array-inputs select { min-width: 70px; font-size: 12px; }
  .field-group { margin-bottom: 14px; }
  .section h2 { font-size: 15px; }
  .section-desc { font-size: 11px; }
}
@media (max-width: 480px) {
  .array-inputs input { width: 60px !important; }
  .field-input input, .field-input select { font-size: 16px; } /* prevent iOS zoom */
}
