:root {
  color-scheme: light;
  --bg: #f4f4f2;
  --surface: #ffffff;
  --surface-2: #f6f5f2;
  --surface-3: #ececE8;
  --text: #17161a;
  --text-secondary: #55535c;
  --muted: #8a8891;
  --border: rgba(23,22,26,0.10);
  --border-strong: rgba(23,22,26,0.18);
  --accent: #2a6fdb;
  --accent-hover: #1c5cc4;
  --accent-soft: rgba(42,111,219,0.10);
  --red: #d8433f;
  --red-soft: rgba(216,67,63,0.10);
  --orange: #e0752c;
  --green: #189171;
  --shadow-sm: 0 1px 2px rgba(23,22,26,0.06);
  --shadow-md: 0 4px 16px rgba(23,22,26,0.08);
  --shadow-lg: 0 12px 40px rgba(23,22,26,0.18);
  --radius: 10px;
  --radius-sm: 6px;
  --section-header-bg: #3a5a80;
  --section-header-fg: #ffffff;
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --bg: #121214;
    --surface: #1c1c1f;
    --surface-2: #232327;
    --surface-3: #2b2b30;
    --text: #f3f2f5;
    --text-secondary: #c2c0c9;
    --muted: #918f98;
    --border: rgba(255,255,255,0.10);
    --border-strong: rgba(255,255,255,0.18);
    --accent: #4c8dee;
    --accent-hover: #6ba1f2;
    --accent-soft: rgba(76,141,238,0.16);
    --red: #ec6c68;
    --red-soft: rgba(236,108,104,0.16);
    --orange: #eb9256;
    --green: #35b892;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.3);
    --shadow-md: 0 4px 16px rgba(0,0,0,0.35);
    --shadow-lg: 0 16px 48px rgba(0,0,0,0.5);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121214;
  --surface: #1c1c1f;
  --surface-2: #232327;
  --surface-3: #2b2b30;
  --text: #f3f2f5;
  --text-secondary: #c2c0c9;
  --muted: #918f98;
  --border: rgba(255,255,255,0.10);
  --border-strong: rgba(255,255,255,0.18);
  --accent: #4c8dee;
  --accent-hover: #6ba1f2;
  --accent-soft: rgba(76,141,238,0.16);
  --red: #ec6c68;
  --red-soft: rgba(236,108,104,0.16);
  --orange: #eb9256;
  --green: #35b892;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.35);
  --shadow-lg: 0 16px 48px rgba(0,0,0,0.5);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", system-ui, sans-serif;
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  height: 100vh;
  -webkit-font-smoothing: antialiased;
}

/* Slim, refined scrollbars everywhere */
* { scrollbar-width: thin; scrollbar-color: var(--border-strong) transparent; }
*::-webkit-scrollbar { width: 9px; height: 9px; }
*::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 8px; border: 2px solid transparent; background-clip: padding-box; }
*::-webkit-scrollbar-track { background: transparent; }

a { color: var(--accent); }

/* ---------------------------------------------------------------- header */
#app-header {
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
}
#app-header h1 {
  font-size: 15px;
  font-weight: 650;
  letter-spacing: -0.01em;
  margin: 0;
}
.status-pills { display: flex; gap: 8px; flex-wrap: wrap; }
.status-pill {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--text-secondary);
  background: var(--surface-2); border: 1px solid var(--border);
  padding: 5px 12px 5px 10px; border-radius: 999px;
  white-space: nowrap;
}
.status-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--muted); flex-shrink: 0;
  box-shadow: 0 0 0 3px transparent;
  transition: background-color 0.2s, box-shadow 0.2s;
}
.status-pill.active .status-dot { background: var(--green); box-shadow: 0 0 0 3px color-mix(in srgb, var(--green) 18%, transparent); }
.status-pill #tdb-status-label, .status-pill #remote-status-label, .tdb-name { color: var(--text); font-weight: 600; }

/* ----------------------------------------------------------------- menu */
#menu-bar {
  display: flex;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 2px 12px;
  gap: 2px;
}
.menu { position: relative; }
.menu-btn {
  background: none; border: none; padding: 8px 12px; cursor: pointer;
  font-size: 13px; color: var(--text-secondary); border-radius: var(--radius-sm);
  transition: background-color 0.12s, color 0.12s;
}
.menu-btn:hover { background: var(--surface-2); color: var(--text); }
.menu-dropdown {
  display: none;
  position: absolute; top: calc(100% + 4px); left: 0;
  background: var(--surface); border: 1px solid var(--border);
  min-width: 230px; z-index: 50; flex-direction: column;
  box-shadow: var(--shadow-lg); border-radius: var(--radius-sm); padding: 6px;
}
.menu-dropdown.open { display: flex; }
.menu-dropdown button {
  background: none; border: none; text-align: left; padding: 8px 10px;
  cursor: pointer; font-size: 13px; color: var(--text); white-space: nowrap;
  border-radius: 6px;
}
.menu-dropdown button:hover { background: var(--accent-soft); color: var(--accent); }
.menu-sep { height: 1px; background: var(--border); margin: 6px 2px; }

/* --------------------------------------------------------------- layout */
#main-layout {
  flex: 1;
  display: flex;
  overflow: hidden;
}
#editor-panel {
  width: 56%;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  overflow: hidden;
}
#tab-body {
  display: flex;
  flex: 1;
  overflow: hidden;
}
#tab-buttons {
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  width: 92px;
  overflow-y: auto;
  background: var(--surface-2);
  border-right: 1px solid var(--border);
  padding: 8px 6px;
  gap: 3px;
}
#tab-buttons button {
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  background: none; border: none; padding: 9px 4px; cursor: pointer;
  color: var(--text-secondary); text-align: center;
  border-radius: var(--radius-sm);
  border-left: 3px solid transparent;
  transition: background-color 0.12s, color 0.12s;
}
#tab-buttons .tab-icon { font-size: 17px; line-height: 1; display: flex; align-items: center; justify-content: center; height: 17px; }
#tab-buttons .tab-icon svg { display: block; }
#tab-buttons .tab-label { font-size: 10.5px; font-weight: 550; letter-spacing: 0.01em; }
#tab-buttons button:hover { background: var(--surface-3); color: var(--text); }
#tab-buttons button.active {
  color: var(--accent); border-left-color: var(--accent); font-weight: 600;
  background: var(--surface); box-shadow: var(--shadow-sm);
}
#tab-content {
  flex: 1;
  overflow-y: auto;
  padding: 16px 18px;
  min-width: 0;
}
#action-buttons {
  padding: 12px 18px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
  background: var(--surface);
}

/* -------------------------------------------------------------- preview */
#preview-panel {
  width: 44%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
  background: var(--surface);
  border: 2px dashed transparent;
  transition: border-color 0.15s, background-color 0.15s;
}
#preview-panel.drag-over {
  border-color: var(--accent);
  background-color: var(--accent-soft);
}
#preview-panel.drag-over::after {
  content: 'Drop input file to load';
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: 600;
  color: var(--accent);
  background: color-mix(in srgb, var(--surface) 85%, transparent);
  pointer-events: none;
  z-index: 10;
}
#preview-header {
  padding: 11px 18px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-secondary);
  letter-spacing: 0.02em;
  text-transform: uppercase;
}
#preview-text {
  flex: 1;
  margin: 0;
  padding: 16px 20px;
  overflow: auto;
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  font-size: 12.5px;
  line-height: 1.6;
  white-space: pre;
  background: var(--surface-2);
}
.pv-comment { color: var(--muted); font-style: italic; }
.pv-section { color: var(--accent); font-weight: 700; }
.pv-param { color: var(--green); }
.pv-value { color: var(--text); font-weight: 600; }

/* ------------------------------------------------------------ statusbar */
#status-bar {
  padding: 6px 18px;
  font-size: 12px;
  background: var(--surface-2);
  border-top: 1px solid var(--border);
  color: var(--text-secondary);
}

/* ------------------------------------------------ section / field atoms */
.section-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 14px;
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}
.section-header {
  background: var(--section-header-bg);
  padding: 10px 14px;
  font-weight: 650;
  font-size: 12.5px;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--section-header-fg);
  border-bottom: 1px solid var(--border);
}
.section-header.collapsible { cursor: pointer; user-select: none; }
.section-header.collapsible:hover { filter: brightness(1.15); }
.section-content { padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.field-row { display: flex; flex-wrap: wrap; gap: 16px; align-items: flex-end; }
.field {
  display: flex; flex-direction: column; gap: 4px; font-size: 13px;
  /* Without this, the default flex-column "stretch" cross-axis alignment
     forces the (short, size=N) input to widen to match its own label's text
     width whenever that label is the wider of the two -- e.g. a label like
     "Range for plotting, min (K):" stretched its own input way out compared
     to a neighboring field whose label ("max (K):") was short. Both label
     and input should size to their own natural width regardless of the
     other's length. */
  align-items: flex-start;
}
.field label { color: var(--text-secondary); font-size: 12px; font-weight: 500; }
/* Scoped by input type (not by ".field ") so any text input/select/textarea
   is styled consistently whether or not it happens to be wrapped in a
   field() -- an unwrapped input (e.g. inline rows like Initial Composition)
   otherwise falls back to the browser's own default border/padding, which
   differs subtly from this styling and visibly shifts on focus as the two
   borders swap. type=checkbox/radio are excluded on purpose -- they must
   keep their native appearance, not this text-field padding/border. */
input[type="text"], input[type="number"], input[type="password"], input:not([type]), select, textarea {
  font-size: 13px; padding: 6px 9px; border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm); background: var(--surface); color: var(--text);
  transition: border-color 0.12s, box-shadow 0.12s;
}
/* <select> renders its native dropdown chrome (arrow icon, internal spacing)
   a bit taller than <input> even with identical padding/font-size/border --
   an explicit height (textareas excluded; they size by `rows`, not this)
   forces every single-line control to line up exactly regardless of that
   native rendering difference. */
input[type="text"], input[type="number"], input[type="password"], input:not([type]), select {
  height: 32px;
}
input[type="text"]:disabled, input[type="number"]:disabled, input[type="password"]:disabled, select:disabled, textarea:disabled {
  background: var(--surface-2); color: var(--muted); cursor: not-allowed;
}
.hint { font-size: 11.5px; color: var(--muted); line-height: 1.4; }
.hint.warn, .accent-red { color: var(--red); }
.accent-blue { color: var(--accent); }
.accent-orange { color: var(--orange); }
.accent-green { color: var(--green); }
.run-case-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; padding: 5px 0; border-bottom: 1px solid var(--border); }
.run-case-row:last-child { border-bottom: none; }
.run-case-name { font-weight: 600; min-width: 90px; }
.run-case-log {
  width: 100%; margin-top: 6px; max-height: 220px; overflow-y: auto; white-space: pre-wrap;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 6px 8px; font-family: ui-monospace, monospace; font-size: 11px;
}
.solvent-label { font-style: italic; color: var(--accent); margin-bottom: 4px; }
.phase-label {
  font-weight: 600; color: var(--accent);
  /* min-width does nothing on an inline element (the default for <span>) --
     without inline-block, longer phase names simply grew past 140px with no
     effect from this rule, so each row's Grains:/Cycles: columns started at
     a different x position depending on that row's own phase name length. */
  display: inline-block; min-width: 180px;
}
.inline-field-label {
  color: var(--text-secondary); font-size: 13px; font-weight: 500;
  display: inline-block; min-width: 110px;
}
.dynamic-group { display: flex; flex-direction: column; gap: 8px; }

h2 { font-size: 16px; font-weight: 650; letter-spacing: -0.01em; margin: 4px 0 2px; }

input, select, textarea, button { font-family: inherit; }
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.phases-textarea, .results-text, .machine-textarea {
  width: 100%; font-family: ui-monospace, monospace; font-size: 12.5px;
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  padding: 8px 10px; background: var(--surface);
}

/* ------------------------------------------------------------- buttons */
button {
  padding: 7px 14px; border-radius: var(--radius-sm); border: 1px solid var(--border-strong);
  background: var(--surface); color: var(--text); cursor: pointer; font-size: 12.5px;
  font-weight: 550; box-shadow: var(--shadow-sm);
  transition: background-color 0.12s, border-color 0.12s, transform 0.08s, box-shadow 0.12s;
}
button:hover { background: var(--surface-2); border-color: var(--border-strong); }
button:active { transform: translateY(1px); }
button.accent { background: var(--accent); color: white; border-color: var(--accent); }
button.accent:hover { background: var(--accent-hover); border-color: var(--accent-hover); box-shadow: var(--shadow-md); }

button:disabled { opacity: 0.5; cursor: not-allowed; box-shadow: none; transform: none; }
button.cancel-btn { border-color: var(--red); color: var(--red); background: var(--red-soft); }
button.cancel-btn:not(:disabled):hover { background: var(--red); color: white; }
button.cancel-btn:disabled { opacity: 0.4; }

/* -------------------------------------------------------------- lists */
.listbox {
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm); max-height: 160px;
  overflow-y: auto; background: var(--surface);
}
.listbox-item { padding: 7px 11px; font-size: 12.5px; cursor: pointer; border-bottom: 1px solid var(--border); transition: background-color 0.1s; }
.listbox-item:last-child { border-bottom: none; }
.listbox-item:hover { background: var(--surface-2); }
.listbox-item.selected { background: var(--accent); color: white; }

.listbox-item-file { display: flex; flex-direction: column; gap: 2px; padding: 6px 11px; }
.file-row-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.file-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-path {
  font-size: 11px; color: var(--text-secondary); font-family: monospace;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.listbox-item-file.selected .file-path { color: rgba(255,255,255,0.85); }
.file-loc-badge {
  flex-shrink: 0; font-size: 10px; font-weight: 700; letter-spacing: 0.03em;
  padding: 2px 7px; border-radius: 999px; text-transform: uppercase;
}
.file-loc-badge.local { background: var(--surface-3); color: var(--text-secondary); }
.file-loc-badge.remote { background: var(--accent-soft); color: var(--accent); }
.listbox-item-file.selected .file-loc-badge.local { background: rgba(255,255,255,0.25); color: white; }
.listbox-item-file.selected .file-loc-badge.remote { background: rgba(255,255,255,0.25); color: white; }

.phase-group-label { font-weight: 650; font-size: 12px; margin-top: 6px; color: var(--text-secondary); text-transform: uppercase; letter-spacing: 0.02em; }
.phase-checkbox-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.phase-checkbox { font-size: 12.5px; display: flex; align-items: center; gap: 5px; }

/* -------------------------------------------------------------- charts */
.chart-container { width: 100%; }
.chart-title { font-size: 13px; font-weight: 650; margin-bottom: 8px; white-space: pre-line; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 14px; margin-top: 10px; }
.legend-item { font-size: 12.5px; display: inline-flex; align-items: center; gap: 5px; }
.legend-swatch { width: 11px; height: 11px; border-radius: 3px; display: inline-block; }

.case-preview-list { max-height: 400px; overflow-y: auto; }

/* --------------------------------------------------------------- busy */
.busy-indicator {
  font-size: 12.5px; color: var(--accent); display: inline-flex; align-items: center; gap: 7px;
  min-height: 18px; font-weight: 550;
}
.spinner {
  display: inline-block; width: 13px; height: 13px; border-radius: 50%;
  border: 2px solid var(--border-strong); border-top-color: var(--accent);
  animation: spin 0.7s linear infinite; flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* -------------------------------------------------------------- modal */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(10,10,12,0.45);
  backdrop-filter: blur(2px);
  display: flex; align-items: center; justify-content: center; z-index: 100;
}
.modal-dialog {
  background: var(--surface); border-radius: var(--radius); min-width: 380px;
  max-width: 90vw; max-height: 85vh; display: flex; flex-direction: column;
  box-shadow: var(--shadow-lg); border: 1px solid var(--border);
}
.modal-dialog.modal-wide { min-width: 600px; }
.modal-header {
  padding: 14px 18px; font-weight: 650; font-size: 14px; border-bottom: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center;
}
.modal-close {
  background: none; border: none; font-size: 20px; line-height: 1; cursor: pointer; padding: 2px 6px;
  border-radius: 6px; color: var(--muted);
}
.modal-close:hover { background: var(--surface-2); color: var(--text); }
.modal-body { padding: 18px; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; }
