/* WebSpice — classic desktop look modelled on LTspice's window layout. */
:root {
  --chrome: #f0f0f0;
  --chrome-2: #ffffff;
  --mdi-bg: #dcdcdc;
  --panel: #ffffff;
  --panel-2: #f3f3f3;
  --border: #c6c6c6;
  --border-strong: #8f8f8f;
  --text: #000000;
  --muted: #5f6368;
  --hover: #e5f1fb;
  --hover-border: #a9d1f5;
  --pressed: #cce4f7;
  --pressed-border: #5ea6e6;
  --accent: #0078d7;
  --accent-ink: #ffffff;
  --accent-soft: rgba(0, 120, 215, 0.12);
  --danger: #c4122f;
  --warn: #9a5800;
  --ok: #107c10;
  --title-active: #ffffff;
  --title-inactive: #f4f4f4;
  --shadow: 0 8px 28px rgba(0, 0, 0, 0.28), 0 1px 3px rgba(0, 0, 0, 0.18);
  --mono: Consolas, "Cascadia Mono", ui-monospace, Menlo, monospace;
  --ui: "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;

  /* schematic (LTspice-style: gray sheet, slate-blue wires and parts, bold black text) */
  --sch-bg: #c8c8c8;
  --sch-grid: #9a9a9a;
  --sch-wire: #34347a;
  --sch-symbol: #34347a;
  --sch-text: #000000;
  --sch-directive: #000000;
  --sch-comment: #0000e0;
  --sch-select: #e0115f;
  --sch-hover: #ffd400;
  --sch-pin: #34347a;
  --sch-flag: #000000;
  --sch-ghost: #6d6d6d;
  --sch-band: rgba(0, 0, 160, 0.10);
  --sch-missing: #c4122f;

  /* waveform viewer (black plot, dashed gray grid, bright traces) */
  --wv-bg: #000000;
  --wv-grid: #6f6f6f;
  --wv-axis: #bdbdbd;
  --wv-text: #f2f2f2;
  --wv-cursor: #ffffff;
  --wv-trace-1: #1fbf6f;
  --wv-trace-2: #3f7fff;
  --wv-trace-3: #ff3b3b;
  --wv-trace-4: #22d3e6;
  --wv-trace-5: #ff4fd8;
  --wv-trace-6: #f2d21b;
  --wv-trace-7: #d9d9d9;
  --wv-trace-8: #ff9a2e;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --chrome: #202124; --chrome-2: #2a2b2e; --mdi-bg: #151618; --panel: #26272a; --panel-2: #2f3034;
  --border: #3c3d41; --border-strong: #5c5e63; --text: #e8eaed; --muted: #9aa0a6;
  --hover: #33373d; --hover-border: #4a5563; --pressed: #2b3d52; --pressed-border: #5e9be0;
  --accent: #4f9dff; --accent-ink: #0b1220; --accent-soft: rgba(79, 157, 255, 0.16);
  --danger: #ff6b81; --warn: #f0a04b; --ok: #3fb950; --title-active: #2a2b2e; --title-inactive: #232427;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.6), 0 2px 6px rgba(0, 0, 0, 0.4);
  --sch-bg: #1b1c1f; --sch-grid: #45474c; --sch-wire: #8ea2ff; --sch-symbol: #b7c1ff; --sch-text: #f1f3f4;
  --sch-directive: #ffffff; --sch-comment: #8fb4ff; --sch-select: #ff4f8b; --sch-hover: #fbbf24; --sch-pin: #b7c1ff;
  --sch-flag: #f1f3f4; --sch-ghost: #8b9099; --sch-band: rgba(122, 162, 255, 0.14); --sch-missing: #ff6b81;
  color-scheme: dark;
}

:root[data-plot="white"] {
  --wv-bg: #ffffff; --wv-grid: #cfcfcf; --wv-axis: #4d4d4d; --wv-text: #000000; --wv-cursor: #444444;
  --wv-trace-1: #0a8f4a; --wv-trace-2: #1f4fd1; --wv-trace-3: #c4122f; --wv-trace-4: #0f8a8a;
  --wv-trace-5: #a2149d; --wv-trace-6: #9a7a00; --wv-trace-7: #3b3b3b; --wv-trace-8: #c45f06;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--chrome);
  color: var(--text);
  font: 12px/1.35 var(--ui);
  overflow: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
kbd {
  font: 11px var(--mono);
  padding: 0 4px;
  border: 1px solid var(--border-strong);
  border-radius: 3px;
  background: var(--panel-2);
}
.mono { font-family: var(--mono); }

.app {
  display: grid;
  grid-template-rows: auto auto auto auto 1fr auto auto;
  height: 100vh;
  height: 100dvh;
}

/* ---------------------------------------------------------------- title, menus */
.titlebar {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 30px;
  padding: 0 10px;
  background: var(--chrome-2);
  border-bottom: 1px solid var(--border);
  min-width: 0;
}
.app-icon { width: 18px; height: 18px; flex: none; }
.win-title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 12.5px; }
.theme-toggle {
  margin-left: auto;
  height: 22px;
  padding: 0 10px;
  border: 1px solid var(--border-strong);
  border-radius: 11px;
  background: var(--panel-2);
  color: var(--text);
  font-size: 11.5px;
  cursor: pointer;
}
.theme-toggle:hover { background: var(--hover); border-color: var(--hover-border); }
.engine-state { display: flex; align-items: center; gap: 6px; color: var(--muted); white-space: nowrap; }
.engine-state .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--warn); }
.engine-state.ready .dot { background: var(--ok); }
.engine-state.busy .dot { background: var(--accent); animation: pulse 0.9s infinite alternate; }
.engine-state.error .dot { background: var(--danger); }
@keyframes pulse { from { opacity: 0.35; } to { opacity: 1; } }

.menubar {
  display: flex;
  height: 22px;
  padding: 0 4px;
  background: var(--chrome-2);
}
.menu { position: relative; }
.menu > button {
  height: 22px;
  border: 1px solid transparent;
  background: none;
  padding: 0 7px;
  cursor: default;
}
.menu > button:hover, .menu.open > button { background: var(--hover); border-color: var(--hover-border); }
.menu-list, .ctx-menu {
  position: absolute;
  top: 100%;
  left: 0;
  min-width: 240px;
  padding: 3px;
  background: var(--panel);
  border: 1px solid var(--border-strong);
  box-shadow: 2px 3px 8px rgba(0, 0, 0, 0.22);
  z-index: 60;
  display: none;
}
.menu.open .menu-list { display: block; }
.ctx-menu { position: fixed; display: block; }
.menu-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 28px;
  width: 100%;
  height: 24px;
  padding: 0 10px 0 28px;
  border: 1px solid transparent;
  background: none;
  text-align: left;
  cursor: default;
  position: relative;
}
.menu-item:hover:not(:disabled) { background: var(--hover); border-color: var(--hover-border); }
.menu-item:disabled { color: var(--muted); }
.menu-item .key { color: var(--muted); }
.menu-item.checked::before { content: "✓"; position: absolute; left: 9px; }
.menu-sep { height: 1px; background: var(--border); margin: 3px 2px 3px 26px; }
.menu-head { padding: 4px 10px 2px 28px; font-size: 11px; color: var(--muted); }

/* ---------------------------------------------------------------- toolbar */
.toolbar {
  display: flex;
  align-items: center;
  gap: 1px;
  padding: 2px 4px;
  background: var(--chrome-2);
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  scrollbar-width: thin;
}
.tb-btn {
  flex: none;
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border: 1px solid transparent;
  background: none;
  cursor: default;
  color: var(--text);
}
.tb-btn svg { width: 24px; height: 24px; }
.tb-btn:hover { background: var(--hover); border-color: var(--hover-border); }
.tb-btn:active, .tb-btn.active { background: var(--pressed); border-color: var(--pressed-border); }
.tb-btn:disabled { opacity: 0.35; background: none; border-color: transparent; }
.tb-sep { flex: none; width: 1px; height: 24px; background: var(--border); margin: 0 3px; }

/* document tabs (one per window, like LTspice's tab strip) */
.doc-tabs {
  display: flex;
  gap: 2px;
  padding: 3px 6px 0;
  background: var(--chrome);
  border-bottom: 1px solid var(--border);
}
.doc-tab {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 22px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-bottom: 0;
  background: var(--panel-2);
  cursor: default;
  font-size: 11.5px;
}
.doc-tab.active { background: var(--panel); font-weight: 600; }
.doc-tab svg { width: 13px; height: 13px; }

/* ---------------------------------------------------------------- MDI workspace */
.mdi {
  display: flex;
  min-height: 0;
  min-width: 0;
  gap: 0;
  padding: 4px;
  background: var(--mdi-bg);
}
.mdi[data-layout="side"] { flex-direction: row; }
.mdi[data-layout="stack"] { flex-direction: column-reverse; }
.mdi-win {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--panel);
  border: 1px solid var(--border-strong);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
}
#schematicPane { flex: 1 1 0; }
#plotPane { flex: 0 0 var(--plot-size, 48%); }
.mdi[data-max="schematic"] #plotPane, .mdi[data-max="schematic"] .splitter { display: none; }
.mdi[data-max="plot"] #schematicPane, .mdi[data-max="plot"] .splitter { display: none; }
.mdi[data-max="plot"] #plotPane { flex: 1 1 0; }
.mdi-win[hidden], .splitter[hidden] { display: none !important; }
.mdi-title {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 26px;
  padding: 0 2px 0 8px;
  background: var(--title-inactive);
  border-bottom: 1px solid var(--border);
  color: var(--muted);
  white-space: nowrap;
  min-width: 0;
}
.mdi-win.active .mdi-title { background: var(--title-active); color: var(--text); }
.mdi-title .spacer { flex: 1; }
.doc-icon { width: 15px; height: 15px; flex: none; }
.dirty { color: var(--text); margin-left: 1px; }
.run-info { color: var(--muted); overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.win-btn {
  height: 22px;
  min-width: 26px;
  padding: 0 6px;
  border: 1px solid transparent;
  background: none;
  cursor: default;
  color: var(--text);
  font-size: 12px;
}
.win-btn:hover { background: var(--hover); border-color: var(--hover-border); }
.win-btn.text { font-size: 11.5px; }
#closePlotBtn:hover, #closeLogBtn:hover { background: #e81123; border-color: #e81123; color: #fff; }
.live-toggle { display: flex; align-items: center; gap: 4px; padding: 0 6px; color: var(--text); font-size: 11.5px; }
.live-toggle input { margin: 0; }
.mdi-body { flex: 1; min-height: 0; position: relative; overflow: hidden; }
.schematic-body { background: var(--sch-bg); }
.plot-host { background: var(--wv-bg); }
.splitter { flex: none; background: transparent; }
.mdi[data-layout="side"] .splitter { width: 5px; cursor: col-resize; }
.mdi[data-layout="stack"] .splitter { height: 5px; cursor: row-resize; }
.splitter:hover, .splitter.dragging { background: var(--pressed-border); }
#schematic { display: block; outline: none; touch-action: none; }

.empty-hint {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  text-align: center;
  pointer-events: none;
  padding: 16px;
  color: #1f1f1f;
}
.empty-hint h2 { margin: 0 0 6px; font-size: 17px; font-weight: 600; }
.empty-hint p { margin: 0 auto 14px; max-width: 460px; line-height: 1.9; color: #333; }
:root[data-theme="dark"] .empty-hint, :root[data-theme="dark"] .empty-hint p { color: var(--text); }
.empty-hint .hint-actions { display: flex; gap: 8px; justify-content: center; pointer-events: auto; }

/* ---------------------------------------------------------------- log + status */
.log-panel { display: flex; flex-direction: column; max-height: 30vh; border-top: 1px solid var(--border-strong); background: var(--panel); }
.log-body { overflow: auto; padding: 4px 10px 8px; font: 12px/1.5 var(--mono); white-space: pre-wrap; }
.log-entry.error { color: var(--danger); }
.log-entry.warn { color: var(--warn); }
.log-entry.meas { color: var(--ok); }
.log-entry.dim { color: var(--muted); }
.log-entry .t { color: var(--muted); margin-right: 8px; }

.statusbar {
  display: flex;
  align-items: center;
  gap: 16px;
  height: 22px;
  padding: 0 8px;
  background: var(--chrome);
  border-top: 1px solid var(--border);
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
}
.statusbar .spacer { flex: 1; }
#statusHint { overflow: hidden; text-overflow: ellipsis; color: var(--muted); }
#statusCoords { color: var(--muted); }

/* ---------------------------------------------------------------- buttons & forms (Windows-like) */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 76px;
  height: 26px;
  padding: 0 12px;
  border: 1px solid #adadad;
  background: #e1e1e1;
  cursor: default;
}
.btn:hover { background: #e5f1fb; border-color: #0078d7; }
.btn.primary { border-color: #0078d7; box-shadow: inset 0 0 0 1px #0078d7; }
:root[data-theme="dark"] .btn { background: #3a3b3f; border-color: #5c5e63; }
:root[data-theme="dark"] .btn:hover { background: #44464b; border-color: var(--accent); }
.btn.small { height: 22px; min-width: 0; padding: 0 8px; font-size: 11.5px; }

input[type="text"], input[type="search"], input:not([type]), select, textarea {
  width: 100%;
  padding: 3px 6px;
  border: 1px solid #7a7a7a;
  border-radius: 0;
  background: var(--panel);
  outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
textarea { font-family: var(--mono); font-size: 12.5px; resize: vertical; }
label.field { display: grid; gap: 3px; color: var(--text); }
label.field > span { font-weight: 400; }
label.field small { color: var(--muted); font-size: 11px; }
.row { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 8px 10px; align-items: start; }
.check { display: flex; align-items: center; gap: 6px; }

/* ---------------------------------------------------------------- dialogs */
dialog.dlg {
  width: min(560px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  padding: 0;
  border: 1px solid #6f6f6f;
  border-radius: 0;
  background: var(--chrome);
  color: var(--text);
  box-shadow: var(--shadow);
}
dialog.dlg.wide { width: min(860px, calc(100vw - 32px)); }
dialog.dlg::backdrop { background: rgba(0, 0, 0, 0.18); }
.dlg-form { display: flex; flex-direction: column; max-height: calc(100vh - 50px); }
.dlg-head { display: flex; align-items: center; gap: 10px; height: 32px; padding: 0 0 0 12px; background: var(--chrome-2); border-bottom: 1px solid var(--border); }
.dlg-head h3 { margin: 0; font-size: 12.5px; font-weight: 400; display: inline; }
.dlg-head .sub { display: inline; margin-left: 8px; color: var(--muted); }
.dlg-head .icon-btn { margin-left: auto; width: 44px; height: 31px; border: 0; background: none; cursor: default; }
.dlg-head .icon-btn:hover { background: #e81123; color: #fff; }
.dlg-body { padding: 12px 14px; overflow: auto; display: grid; gap: 10px; }
.dlg-foot { display: flex; gap: 8px; justify-content: flex-end; align-items: center; padding: 10px 14px; border-top: 1px solid var(--border); }
.dlg-foot .left { margin-right: auto; color: var(--muted); }
.icon-btn { border: 0; background: none; cursor: default; color: var(--text); }
.tabs { display: flex; gap: 0; border-bottom: 1px solid var(--border-strong); }
.tab {
  border: 1px solid transparent;
  border-bottom: 0;
  background: none;
  padding: 4px 12px;
  margin-bottom: -1px;
  cursor: default;
}
.tab:hover { background: var(--hover); }
.tab.active { background: var(--panel); border-color: var(--border-strong); border-bottom: 1px solid var(--panel); }
.tab-panel { display: grid; gap: 10px; }
.tab-panel[hidden] { display: none; }
.preview-line {
  font-family: var(--mono);
  padding: 6px 8px;
  background: var(--panel);
  border: 1px solid var(--border);
  word-break: break-all;
}
.help-text { color: var(--muted); margin: 0; }

.picker { display: grid; grid-template-columns: 1fr 240px; gap: 12px; min-height: 360px; }
.picker-list { overflow: auto; max-height: 52vh; border: 1px solid #7a7a7a; background: var(--panel); }
.picker-cat { padding: 4px 8px 3px; font-size: 11px; font-weight: 600; color: var(--muted); background: var(--panel-2); position: sticky; top: 0; }
.picker-item { display: flex; justify-content: space-between; gap: 8px; width: 100%; padding: 3px 8px; border: 0; background: none; text-align: left; cursor: default; }
.picker-item:hover { background: var(--hover); }
.picker-item.active { background: var(--accent); color: var(--accent-ink); }
.picker-item .k { font-family: var(--mono); }
.picker-item .d { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.picker-item.active .d { color: inherit; }
.picker-preview { display: grid; gap: 6px; align-content: start; }
.picker-preview canvas { width: 100%; aspect-ratio: 1; border: 1px solid #7a7a7a; background: var(--sch-bg); }
.picker-preview .desc { color: var(--muted); }

.examples { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 8px; }
.example-card {
  display: grid;
  gap: 4px;
  padding: 10px;
  text-align: left;
  border: 1px solid var(--border);
  background: var(--panel);
  cursor: default;
}
.example-card:hover { background: var(--hover); border-color: var(--hover-border); }
.example-card strong { font-size: 12.5px; }
.example-card span { color: var(--muted); }
.example-card code { font: 11.5px var(--mono); color: var(--accent); }

table.data { width: 100%; border-collapse: collapse; background: var(--panel); }
table.data th, table.data td { padding: 3px 8px; border: 1px solid var(--border); text-align: left; }
table.data td.num { font-family: var(--mono); text-align: right; }
table.data th { background: var(--panel-2); font-weight: 600; }

.vector-list { max-height: 40vh; overflow: auto; border: 1px solid #7a7a7a; background: var(--panel); display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); padding: 2px; }
.vector-list button { border: 0; background: none; padding: 3px 6px; text-align: left; cursor: default; font: 12px var(--mono); }
.vector-list button:hover { background: var(--accent); color: var(--accent-ink); }

pre.netlist { margin: 0; padding: 8px 10px; max-height: 60vh; overflow: auto; background: var(--panel); border: 1px solid #7a7a7a; font: 12px/1.5 var(--mono); }
.shortcut-grid { display: grid; grid-template-columns: auto 1fr; gap: 5px 14px; }
.shortcut-grid kbd { justify-self: start; }

/* ---------------------------------------------------------------- component creation */
.inline { display: flex; gap: 6px; }
.inline input { flex: 1; }
.found-list { display: grid; gap: 4px; max-height: 140px; overflow: auto; padding: 6px 8px; border: 1px solid var(--border); background: var(--panel); }
.found-list .check { font-family: var(--mono); font-size: 12px; }
.sym-editor { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 14px; }
.sym-left { display: grid; gap: 6px; align-content: start; min-width: 0; }
.sym-tools { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.sym-tools .btn { min-width: 0; }
.sym-tools .sym-tool.active { background: var(--pressed); border-color: var(--pressed-border); box-shadow: inset 0 0 0 1px var(--pressed-border); }
.sym-tools select { width: auto; max-width: 220px; }
.sym-canvas { width: 100%; height: 420px; border: 1px solid #7a7a7a; background: var(--sch-bg); cursor: crosshair; outline: none; touch-action: none; }
.sym-right { align-content: start; }
.pin-list { display: grid; gap: 3px; max-height: 180px; overflow: auto; padding: 4px; border: 1px solid var(--border); background: var(--panel); }
.pin-row { display: grid; grid-template-columns: 22px 1fr auto auto auto; gap: 3px; align-items: center; }
.pin-no { color: var(--muted); font: 11px var(--mono); text-align: right; }
.comp-list { display: grid; gap: 6px; }
.comp-row { display: grid; grid-template-columns: 64px 1fr auto; gap: 8px; align-items: center; padding: 6px; border: 1px solid var(--border); background: var(--panel); }
.comp-actions { display: flex; flex-wrap: wrap; gap: 4px; justify-content: flex-end; }
.comp-actions .btn { min-width: 0; }
.comp-thumb { width: 64px; height: 64px; border: 1px solid var(--border); background: var(--sch-bg); }
.comp-info { display: grid; gap: 2px; min-width: 0; }
.comp-info strong { font-family: var(--mono); font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
@media (max-width: 760px) {
  .sym-editor { grid-template-columns: 1fr; }
  .sym-canvas { height: 300px; }
  .comp-row { grid-template-columns: 48px 1fr; }
  .comp-actions { grid-column: 1 / -1; justify-content: flex-start; }
}

/* ---------------------------------------------------------------- waveform viewer */
.wv-legend { justify-content: center; padding: 3px 10px; gap: 2px 18px; min-height: 22px; background: var(--wv-bg); }
.wv-legend-item { font-weight: 700; font-size: 12px; cursor: default; }
.wv-readout {
  font: 11.5px/1.45 var(--mono);
  background: rgba(20, 20, 20, 0.92);
  color: #f2f2f2;
  border: 1px solid #6f6f6f;
  padding: 4px 7px;
}
:root[data-plot="white"] .wv-readout { background: #ffffe1; color: #000; border-color: #767676; }

/* ---------------------------------------------------------------- toasts */
.toast-stack { position: fixed; right: 14px; bottom: 32px; display: grid; gap: 6px; z-index: 100; pointer-events: none; }
.toast {
  max-width: min(420px, calc(100vw - 28px));
  padding: 7px 11px;
  background: #ffffe1;
  color: #000;
  border: 1px solid #767676;
  box-shadow: 2px 3px 8px rgba(0, 0, 0, 0.2);
  animation: toast-in 0.15s ease-out;
}
.toast.error { background: #fde7e9; border-color: #c4122f; }
@keyframes toast-in { from { transform: translateY(6px); opacity: 0; } }

/* hard-coded ink colours in the toolbar icons need flipping on the dark scheme */
:root[data-theme="dark"] .tb-btn svg, :root[data-theme="dark"] .doc-tab svg { filter: invert(1) hue-rotate(180deg); }

@media print {
  .titlebar, .menubar, .toolbar, .doc-tabs, .statusbar, .log-panel, .mdi-title, .splitter, #plotPane, .toast-stack { display: none !important; }
  .app { display: block; height: auto; }
  .mdi { padding: 0; background: none; }
  #schematicPane { border: 0; box-shadow: none; }
}

@media (max-width: 760px) {
  .engine-state .label { display: none; }
  .mdi[data-layout="side"] { flex-direction: column-reverse; }
  .mdi[data-layout="side"] .splitter { width: auto; height: 5px; cursor: row-resize; }
  .live-toggle, .win-btn.text { display: none; }
  .picker { grid-template-columns: 1fr; }
  .picker-preview { display: none; }
}
