:root {
  --bg: #0f1420;
  --card: #182030;
  --card2: #1e2740;
  --ink: #e6ecf5;
  --muted: #9aa7bd;
  --line: #2c3750;
  --accent: #4da3ff;
  --accent2: #2b6fd6;
  --ok: #3ecb8c;
  --warn: #f0b34a;
  --err: #ff6b6b;
  /* shared component palette — used by BOTH the schematic and the .ibs output */
  --c-pu:   #f0b34a;   /* Pull-up  / PMOS   */
  --c-pd:   #4da3ff;   /* Pull-down / NMOS  */
  --c-pwr:  #3ecb8c;   /* POWER clamp diode */
  --c-gnd:  #b98cff;   /* GND clamp diode   */
  --c-cc:   #2fc2c6;   /* C_comp / C_pkg    */
  --c-pkg:  #9aa7bd;   /* package R/L       */
  --c-ramp: #ff9e64;   /* ramp              */
}
* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  line-height: 1.5;
}
header { padding: 2rem 1.25rem 0.5rem; max-width: 900px; margin: 0 auto; position: relative; }
.rev {
  position: absolute; top: 1.5rem; right: 1.25rem;
  font-size: 0.72rem; font-family: ui-monospace, Menlo, monospace;
  color: var(--muted); text-decoration: none;
  border: 1px solid var(--line); border-radius: 999px; padding: 0.15rem 0.6rem;
}
.rev:hover { border-color: var(--accent); color: var(--accent); }
h1 { margin: 0; font-size: 1.7rem; letter-spacing: -0.02em; }
.tag { color: var(--muted); max-width: 60ch; }
code { background: rgba(255,255,255,0.07); padding: 0.05em 0.35em; border-radius: 4px; font-size: 0.9em; }

.disclaimer {
  max-width: 900px; margin: 0.5rem auto 1rem; padding: 0.75rem 1rem;
  background: rgba(240,179,74,0.12); border: 1px solid rgba(240,179,74,0.4);
  border-radius: 10px; color: #f3d9a8; font-size: 0.9rem;
}
.disclaimer strong { color: var(--warn); }

main { max-width: 900px; margin: 0 auto; padding: 0 1.25rem 3rem; }

.toc {
  background: var(--card); border: 1px solid var(--line);
  border-radius: 14px; padding: 0.9rem 1.25rem; margin-bottom: 1.25rem;
}
.toc-h {
  display: block; font-size: 0.72rem; letter-spacing: 0.08em; text-transform: uppercase;
  color: var(--muted); margin-bottom: 0.4rem;
}
.toc ol { margin: 0; padding: 0; list-style: none; display: flex; flex-wrap: wrap; gap: 0.3rem 1.4rem; }
.toc li { font-size: 0.9rem; }
.toc a { color: var(--accent); text-decoration: none; }
.toc a:hover { text-decoration: underline; }
.toc-sub { display: block; margin-top: 0.2rem; font-size: 0.8rem; color: var(--muted); }
.toc-sub a { color: var(--muted); }
.toc-sub a:hover { color: var(--accent); }
html { scroll-behavior: smooth; }
:target { scroll-margin-top: 1rem; }

.card {
  background: var(--card); border: 1px solid var(--line);
  border-radius: 14px; padding: 1.25rem 1.25rem 1.5rem; margin-bottom: 1.25rem;
}
.card h2 { margin: 0 0 1rem; font-size: 1.05rem; color: var(--accent); font-weight: 600; }

.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 0.75rem 1rem; }
label { display: flex; flex-direction: column; font-size: 0.82rem; color: var(--muted); gap: 0.3rem; }
label.full { grid-column: 1 / -1; }
input, select, textarea {
  background: var(--bg); color: var(--ink); border: 1px solid var(--line);
  border-radius: 8px; padding: 0.5rem 0.6rem; font-size: 0.92rem; font-family: inherit;
}
textarea { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); }

details { margin-top: 0.9rem; }
summary { cursor: pointer; color: var(--accent); font-size: 0.85rem; margin-bottom: 0.6rem; }
.note { color: var(--muted); font-size: 0.82rem; font-style: italic; margin: 0.6rem 0 0; }
.pad-help { font-style: normal; }
.pad-help b { color: var(--c-cc); font-style: normal; }
button.mini {
  background: var(--card2); color: var(--accent); border: 1px solid var(--line);
  border-radius: 6px; padding: 0.15rem 0.5rem; font-size: 0.76rem; cursor: pointer;
  margin-left: 0.3rem; font-style: normal;
}
button.mini:hover { border-color: var(--accent); }

.tabs { display: flex; gap: 0.4rem; margin-bottom: 1rem; flex-wrap: wrap; }
.tab {
  background: var(--card2); color: var(--muted); border: 1px solid var(--line);
  padding: 0.5rem 0.9rem; border-radius: 8px; cursor: pointer; font-size: 0.88rem;
}
.tab.active { background: var(--accent2); color: #fff; border-color: var(--accent); }
.panel { display: none; }
.panel.active { display: block; }

button.primary {
  margin-top: 1.25rem; background: linear-gradient(180deg, var(--accent), var(--accent2));
  color: #fff; border: none; padding: 0.7rem 1.3rem; border-radius: 10px;
  font-size: 0.98rem; font-weight: 600; cursor: pointer;
}
button.primary:hover { filter: brightness(1.08); }

button.secondary {
  margin-top: 0.5rem; background: var(--card2); color: var(--ink);
  border: 1px solid var(--accent); padding: 0.55rem 1.1rem; border-radius: 9px;
  font-size: 0.9rem; cursor: pointer;
}
button.secondary:hover { background: var(--accent2); color: #fff; }

.outbar { display: flex; gap: 0.6rem; margin-bottom: 0.8rem; }
.outbar button {
  background: var(--card2); color: var(--ink); border: 1px solid var(--line);
  padding: 0.45rem 0.9rem; border-radius: 8px; cursor: pointer; font-size: 0.85rem;
}
.outbar button:hover { border-color: var(--accent); }

pre {
  background: #0a0e17; border: 1px solid var(--line); border-radius: 10px;
  padding: 1rem; overflow-x: auto; font-size: 0.8rem; line-height: 1.45;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; color: #cdd6e6;
  max-height: 520px; overflow-y: auto;
}

.vmsg { padding: 0.5rem 0.75rem; border-radius: 8px; font-size: 0.86rem; margin-bottom: 0.4rem; }
.vmsg.ok { background: rgba(62,203,140,0.12); color: var(--ok); }
.vmsg.warn { background: rgba(240,179,74,0.12); color: var(--warn); }
.vmsg.err { background: rgba(255,107,107,0.12); color: var(--err); }

/* ---- live diagram ---- */
.livedot { font-size: 0.72rem; color: var(--ok); font-weight: 500; margin-left: 0.5rem; }
.diagwrap { display: flex; gap: 1rem; flex-wrap: wrap; align-items: flex-start; }
.schcol { flex: 2 1 380px; min-width: 300px; }
.ivcol { flex: 1 1 260px; min-width: 240px; }
.schsvg, .ivsvg { width: 100%; height: auto; }

.schsvg .w { stroke: var(--muted); stroke-width: 1.4; fill: none; }
.schsvg [class*="c-"], .schsvg .comp { stroke-width: 1.8; fill: none; }
/* per-component stroke/fill colors (shared palette) */
.schsvg .c-pu   { stroke: var(--c-pu); }   .schsvg .c-pu.fill   { fill: var(--c-pu); }
.schsvg .c-pd   { stroke: var(--c-pd); }   .schsvg .c-pd.fill   { fill: var(--c-pd); }
.schsvg .c-pwr  { stroke: var(--c-pwr); }  .schsvg .c-pwr.fill  { fill: var(--c-pwr); }
.schsvg .c-gnd  { stroke: var(--c-gnd); }  .schsvg .c-gnd.fill  { fill: var(--c-gnd); }
.schsvg .c-cc   { stroke: var(--c-cc); }   .schsvg .c-cc.fill   { fill: var(--c-cc); }
.schsvg .c-pkg  { stroke: var(--c-pkg); }  .schsvg .c-pkg.fill  { fill: var(--c-pkg); }
.schsvg .comp { stroke: var(--accent); }   .schsvg .comp.fill { fill: var(--accent); }
.schsvg .node { fill: var(--accent); stroke: none; }
.schsvg text { fill: var(--ink); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.schsvg .rail { font-size: 12px; fill: var(--muted); }
.schsvg .sub { font-size: 10.5px; fill: var(--muted); }
.schsvg text.node { font-size: 11px; font-weight: 600; fill: var(--ok); }
.schsvg .ramp { font-size: 11px; fill: var(--c-ramp); }
/* device labels take the component color */
.schsvg .t-pu { font-size: 12px; font-weight: 600; fill: var(--c-pu); }
.schsvg .t-pd { font-size: 12px; font-weight: 600; fill: var(--c-pd); }

.ivsvg .ax { stroke: var(--muted); stroke-width: 1; }
.ivsvg text { fill: var(--muted); font-family: ui-monospace, Menlo, monospace; font-size: 10px; }
.ivsvg .axt { font-size: 10px; text-anchor: middle; }
.ivsvg .leg { font-size: 10px; }
.ivsvg .curve { fill: none; stroke-width: 2; }
.ivsvg .cd { stroke: var(--c-pd); }
.ivsvg .cu { stroke: var(--c-pu); }
.ivsvg rect.cd { fill: var(--c-pd); stroke: none; }
.ivsvg rect.cu { fill: var(--c-pu); stroke: none; }

/* ---- transient step-response plot ---- */
.trsvg { width: 100%; height: auto; max-width: 720px; }
.trsvg .ax { stroke: var(--muted); stroke-width: 1; }
.trsvg text { fill: var(--muted); font-family: ui-monospace, Menlo, monospace; }
.trsvg .axt { font-size: 10px; text-anchor: middle; }
.trsvg .leg { font-size: 10px; }
.trsvg .tr-tick { font-size: 9px; text-anchor: end; }
.trsvg .tr-guide { stroke: var(--line); stroke-width: 1; stroke-dasharray: 2 3; }
.trsvg .curve { fill: none; stroke-width: 2; }
.trsvg .curve.cu { stroke: var(--c-pu); }
.trsvg .curve.cd { stroke: var(--c-pd); }
.trsvg .tr-cross { stroke-width: 1.2; stroke-dasharray: 4 3; }
.trsvg .tr-cross.cu { stroke: var(--c-pu); }
.trsvg .tr-cross.cd { stroke: var(--c-pd); }
.trsvg .tr-mark.cu { fill: var(--c-pu); }
.trsvg .tr-mark.cd { fill: var(--c-pd); }
.trsvg .tr-lbl { font-size: 11px; font-weight: 700; }
.trsvg .tr-lbl.cu { fill: var(--c-pu); }
.trsvg .tr-lbl.cd { fill: var(--c-pd); }

/* ---- color-coded .ibs output (matches the schematic palette) ---- */
#output .cmt { color: var(--muted); opacity: 0.65; }
#output .kw  { color: var(--accent); font-weight: 600; }
#output .s-pu   { color: var(--c-pu); }   #output .h-pu   { color: var(--c-pu);  font-weight: 700; }
#output .s-pd   { color: var(--c-pd); }   #output .h-pd   { color: var(--c-pd);  font-weight: 700; }
#output .s-pwr  { color: var(--c-pwr); }  #output .h-pwr  { color: var(--c-pwr); font-weight: 700; }
#output .s-gnd  { color: var(--c-gnd); }  #output .h-gnd  { color: var(--c-gnd); font-weight: 700; }
#output .s-cc   { color: var(--c-cc); }
#output .s-pkg  { color: var(--c-pkg); }  #output .h-pkg  { color: var(--c-pkg); font-weight: 700; }
#output .s-ramp { color: var(--c-ramp); } #output .h-ramp { color: var(--c-ramp); font-weight: 700; }

.legend { display: flex; flex-wrap: wrap; gap: 0.5rem 0.9rem; margin: 0 0 0.7rem; font-size: 0.78rem; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 0.35rem; }
.legend i { width: 11px; height: 11px; border-radius: 3px; display: inline-block; }

footer { max-width: 900px; margin: 0 auto; padding: 0 1.25rem 3rem; color: var(--muted); font-size: 0.8rem; }
footer a { color: var(--accent); }

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f5f7fb; --card: #ffffff; --card2: #eef2f9; --ink: #1a2233;
    --muted: #5b6b85; --line: #d5dced;
    --c-pu: #c77f14; --c-pd: #2b6fd6; --c-pwr: #1f9d6b; --c-gnd: #8a5cf0;
    --c-cc: #0f8f93; --c-pkg: #6b7890; --c-ramp: #d9741f;
  }
  pre { background: #0a0e17; color: #cdd6e6; }
}
