:root {
    --fg: #111;
    --mute: #6b6b6b;
    --rule: #dcdcdc;
    --field: #f4f4f4;
    --bg: #fff;
    --accent: #d9480f;
    --ar: 1.381;   /* schematic aspect; overwritten from the loaded image by app.js */
    --mono: ui-monospace, "Cascadia Mono", Consolas, "SF Mono", Menlo, monospace;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
    background: var(--bg);
    color: var(--fg);
    font: 13px/1.35 "Segoe UI", system-ui, -apple-system, "Hiragino Sans", "Yu Gothic UI", sans-serif;
    font-variant-numeric: tabular-nums;
}

[hidden] { display: none !important; }

sub { font-size: 0.75em; line-height: 0; }

/* ---------- layout ---------- */

.app { display: grid; gap: 12px; padding: 12px; }
.col { display: flex; flex-direction: column; gap: 12px; min-width: 0; min-height: 0; }

.schematic { margin: 0; min-width: 0; }
.schematic .stack { display: block; position: relative; margin: 0 auto; width: min(100%, calc(60vh * var(--ar))); }
.schematic .layer { position: absolute; inset: 0; width: 100%; height: 100%; transition: opacity 0.12s; }
.schematic img {
    display: block;
    width: 100%;
    height: auto;
}

/* 2 columns: [schematic / inputs] | [results / models] */
@media (min-width: 700px) {
    .app {
        height: 100vh;
        grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr);
        gap: 20px;
    }
    /* --ar keeps the box tight to the drawing */
    .schematic .stack { width: min(100%, calc(52vh * var(--ar))); }
    .col-r { overflow: auto; border-left: 1px solid var(--rule); padding-left: 20px; }
}

/* 3 columns: schematic | inputs + models | results */
@media (min-width: 1400px) {
    .app {
        grid-template-columns: minmax(0, 1fr) 400px 440px;
        grid-template-rows: auto minmax(0, 1fr);
        grid-template-areas: "sch inp res" "sch mod res";
        gap: 0 24px;
        padding: 16px 20px;
    }
    .col, .col-r { display: contents; }
    .schematic { grid-area: sch; min-height: 0; align-self: center; }
    .schematic .stack { width: min(100%, calc((100vh - 32px) * var(--ar))); }
    .inputs { grid-area: inp; }
    .models { grid-area: mod; margin-top: 8px; }
    .results { grid-area: res; overflow: auto; }
    .inputs, .models, .results { border-left: 1px solid var(--rule); padding-left: 24px; }
    .app .model textarea { height: calc(8 * 11px * 1.35 + 5px); }
}

/* ---------- inputs ---------- */

.inputs { display: flex; flex-direction: column; gap: 8px; margin: 0; }

.seg {
    display: flex;
    margin: 0;
    padding: 0;
    border: 1px solid var(--fg);
    width: max-content;
}
.seg label { position: relative; cursor: pointer; }
.seg input {
    position: absolute;
    opacity: 0;
    width: 1px;
    height: 1px;
    margin: 0;
}
.seg span { display: block; padding: 3px 14px; }
.seg input:checked + span { background: var(--fg); color: var(--bg); }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 1px; }

.fields {
    display: grid;
    /* label | value (max ~9em) | unit + gap | label | value | unit */
    grid-template-columns: 3.6em minmax(0, 9em) 2.6em 3.6em minmax(0, 9em) 1.6em;
    justify-content: start;
    gap: 4px 6px;
    align-items: center;
    padding-top: 8px;
    border-top: 1px solid var(--rule);
}

.fields label { white-space: nowrap; }
.u { color: var(--mute); }

input, select, textarea {
    font: inherit;
    color: inherit;
    background: var(--field);
    border: 1px solid transparent;
    border-radius: 0;
    min-width: 0;
    width: 100%;
    margin: 0;
}
input, select { height: 24px; padding: 0 6px; text-align: right; }
/* Own chevron: WebKit on iPadOS draws the native arrow over right-aligned text. */
select {
    -webkit-appearance: none;
    appearance: none;
    padding: 0 18px 0 6px;
    text-align: right;
    text-align-last: right;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='8' height='5'%3E%3Cpath d='M0 0l4 5 4-5z' fill='%236b6b6b'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 6px center;
    background-size: 8px 5px;
}
input::placeholder { color: var(--mute); }

input:focus, select:focus, textarea:focus {
    outline: 2px solid var(--accent);
    outline-offset: -1px;
    background: var(--bg);
}

.bad { border-color: var(--accent); }

.pair { display: flex; align-items: center; gap: 4px; min-width: 0; }
.pair input { text-align: center; }
.pair i { font-style: normal; color: var(--mute); }
.pair.ratio { grid-column: span 4; }
.seg2 {
    display: flex;
    flex: none;
    height: 24px;
    padding: 0;
    font: inherit;
    font-size: 12px;
    color: var(--fg);
    background: var(--bg);
    border: 1px solid var(--fg);
    border-radius: 0;
    cursor: pointer;
}
.seg2 span { display: flex; align-items: center; padding: 0 7px; }
.seg2[aria-pressed="false"] span:first-child,
.seg2[aria-pressed="true"] span:last-child { background: var(--fg); color: var(--bg); }
.seg2:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.models {
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding-top: 8px;
    border-top: 1px solid var(--rule);
}
.model {
    display: grid;
    grid-template-columns: 4.2em minmax(0, 1fr);
    gap: 2px 6px;
}
.model label { padding-top: 3px; }
.model textarea {
    font: 11px/1.35 var(--mono);
    padding: 3px 5px 0;
    height: calc(4 * 11px * 1.35 + 5px);
    resize: vertical;
}
.parsed {
    grid-column: 2;
    font: 10.5px/1.3 var(--mono);
    color: var(--mute);
    white-space: pre-wrap;
}

/* ---------- results ---------- */

.status {
    min-height: 1.35em;
    color: var(--mute);
    font-size: 11px;
    font-family: var(--mono);
    margin-bottom: 4px;
}
.status.err {
    color: var(--accent);
    font: 600 13px/1.35 "Segoe UI", system-ui, -apple-system, "Hiragino Sans", "Yu Gothic UI", sans-serif;
}

#out.stale { opacity: 0.35; }

table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 10px;
}
th, td {
    padding: 2px 6px;
    border-bottom: 1px solid var(--rule);
    white-space: nowrap;
}
th { text-align: left; font-weight: 400; color: var(--mute); }
td { text-align: right; }
thead th { font-size: 11px; text-align: right; }
thead th:first-child { text-align: left; }

table.primary td { font-size: 15px; font-weight: 600; }
table.primary th { color: var(--fg); font-size: 15px; }
table.primary td + td { font-weight: 400; font-size: 13px; color: var(--mute); }

/* results: compact label-value pairs, sized to content (calculator only) */
#out table { table-layout: fixed; width: calc(2 * (4em + 7.6em)); }
#out col.cl { width: 4em; }
#out col.cv { width: 7.6em; }
#out th:not(:first-child) { padding-left: 14px; }
#out td { overflow: hidden; text-overflow: ellipsis; }
.fit-par th { width: auto; }

.warn {
    margin: 0;
    padding: 0;
    list-style: none;
    font-size: 12px;
}
.warn li { padding-left: 1em; text-indent: -1em; }
.warn li::before { content: "! "; color: var(--accent); font-weight: 700; }


.hot { color: var(--accent); font-weight: 600; }

/* R_BC: [OPEN][value] */
.bc input { text-align: right; }
.open {
    font-weight: 600;
    font-size: 10px;
    line-height: 1;
    font-family: inherit;
    letter-spacing: 0.04em;
    height: 24px;
    padding: 0 6px;
    color: var(--mute);
    background: var(--bg);
    border: 1px solid var(--rule);
    border-radius: 0;
    cursor: pointer;
    flex: none;
}
.open[aria-pressed="true"] { color: var(--bg); background: var(--fg); border-color: var(--fg); }
.open:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
input.off { color: var(--mute); background: transparent; border-color: var(--rule); }


/* CSV buttons: last in the DOM (Tab order), shown at the top-right of the results */
.csvbar { display: flex; gap: 4px; justify-content: flex-end; }
.csvbar .open { height: 20px; }
@media (min-width: 700px) {
    .app { position: relative; }
    .csvbar { position: absolute; top: 12px; right: 12px; }
    .status { padding-right: 220px; }
}
@media (min-width: 1400px) {
    .csvbar { top: 16px; right: 20px; }
}
body.drop::after {
    content: "";
    position: fixed;
    inset: 0;
    border: 3px solid var(--accent);
    pointer-events: none;
}
.open:disabled { color: var(--rule); border-color: var(--rule); cursor: default; }

/* SPICE parameter dialog */
.mhead { display: flex; flex-direction: column; align-items: flex-start; gap: 4px; }
.mhead .open { height: 20px; }
dialog {
    padding: 14px 16px;
    border: 1px solid var(--fg);
    color: var(--fg);
    background: var(--bg);
    font: inherit;
    width: min(520px, calc(100vw - 32px));
}
dialog::backdrop { background: rgba(0, 0, 0, 0.25); }
.ptitle { font-weight: 600; margin-bottom: 10px; }
.pgrid {
    display: grid;
    grid-template-columns: repeat(4, 2.8em minmax(0, 1fr));
    gap: 6px 8px;
    align-items: center;
}
.pgrid label { font: 11px var(--mono); color: var(--mute); }
.pgrid input { font-family: var(--mono); font-size: 12px; }
.pbtns { display: flex; gap: 6px; align-items: center; margin-top: 14px; padding-top: 10px; border-top: 1px solid var(--rule); }
.pbtns .open { height: 24px; padding: 0 10px; }
.pbtns #papply { color: var(--bg); background: var(--fg); border-color: var(--fg); }
.pbtns #papply:disabled { color: var(--bg); background: var(--rule); border-color: var(--rule); }
.perr { flex: 1; color: var(--accent); font-weight: 600; font-size: 12px; }
@media (max-width: 520px) { .pgrid { grid-template-columns: repeat(2, 2.8em minmax(0, 1fr)); } }

/* ---------- fit.html ---------- */
.fit { display: grid; gap: 16px; padding: 12px; }
.fit-in, .fit-out { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.fit-top { display: flex; gap: 6px; }
a.open { display: inline-flex; align-items: center; height: 20px; text-decoration: none; }
.meas { margin: 0; }
.meas th, .meas td { padding: 2px 3px; }
.meas thead th { text-align: center; font-size: 12px; }
.meas tbody th { width: 1.6em; text-align: right; color: var(--mute); }
.meas input { height: 22px; }
.hint { display: flex; gap: 4px; }
.fit-opt { grid-template-columns: 4.2em minmax(0, 8em) 1.4em; border-top: 1px solid var(--rule); }
.ovr { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 4px; }
.ovr-b {
    display: flex; justify-content: space-between; align-items: center;
    height: 24px; padding: 0 8px; font: inherit; font-size: 12px;
    color: var(--fg); background: var(--bg); border: 1px solid var(--rule); border-radius: 0; cursor: pointer;
}
.ovr-b b { font-weight: 400; color: var(--mute); }
.ovr-b[data-s="free"] { border-color: var(--fg); }
.ovr-b[data-s="free"] b { color: var(--fg); font-weight: 600; }
.ovr-b[data-s="fix"] { background: var(--fg); color: var(--bg); border-color: var(--fg); }
.ovr-b[data-s="fix"] b { color: var(--bg); font-weight: 600; }
.ovr-b:focus-visible, .base summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.base summary { cursor: pointer; color: var(--mute); font-size: 12px; }
.base textarea, #fit-model { width: 100%; font: 11px/1.35 var(--mono); padding: 3px 5px; resize: vertical; }
.base textarea { margin: 4px 0; }
.fit-plot { min-width: 0; min-height: 0; }
.fit-plot svg { display: block; width: 100%; max-width: 720px; height: auto; max-height: 100%; margin: 0 auto; }
.fit-plot .stale, #fit-res.stale { opacity: 0.35; }
#plot .g { stroke: var(--rule); stroke-width: 0.6; }
#plot .ax { stroke: var(--mute); stroke-width: 1; }
#plot .t { fill: var(--mute); font-size: 10px; font-family: inherit; }
#plot .m { fill: none; stroke: var(--accent); stroke-width: 1.6; }
#plot .d { fill: var(--fg); }
.fit-par td.fs { color: var(--mute); font-size: 11px; text-align: left; width: 3.5em; }
.fit-par th { width: auto; }
.fit-pts td, .fit-pts th { font-size: 12px; }
.fit-pts thead th { text-align: right; }
#fit-model { height: calc(4 * 11px * 1.35 + 8px); background: var(--field); }
.fit-btns { display: flex; gap: 4px; }
.fit-btns .open { height: 24px; padding: 0 10px; }
@media (min-width: 700px) {
    .fit {
        height: 100vh;
        grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
        grid-template-rows: auto minmax(0, 1fr);
        grid-template-areas: "in out" "plot out";
    }
    .fit-in { grid-area: in; }
    .fit-plot { grid-area: plot; }
    .fit-out { grid-area: out; overflow: auto; border-left: 1px solid var(--rule); padding-left: 16px; }
}
@media (min-width: 1400px) {
    .fit {
        grid-template-columns: 440px minmax(0, 1fr) 460px;
        grid-template-rows: minmax(0, 1fr);
        grid-template-areas: "in plot out";
        padding: 16px 20px;
        gap: 24px;
    }
    .fit-plot { align-self: center; }
    .fit-in { overflow: auto; }
}
.open.target { color: var(--bg); background: var(--fg); border-color: var(--fg); }
.mhead a.open { height: 20px; }
.open.target:disabled { color: var(--bg); background: var(--rule); border-color: var(--rule); }

/* Bode dialog */
dialog.bode {
    position: fixed;
    inset: auto;
    margin: 0;
    padding: 0 12px 10px;
    min-width: 320px;
    min-height: 240px;
    resize: both;
    overflow: hidden;
    z-index: 10;
}
dialog.bode[open] { display: flex; flex-direction: column; }
dialog.bode form { flex: 1; min-height: 0; }
.btitle {
    display: flex; align-items: center; gap: 4px;
    margin: 0 -12px 8px; padding: 3px 6px 3px 12px;
    border-bottom: 1px solid var(--rule);
    cursor: move; user-select: none; touch-action: none;
    color: var(--mute); font-size: 11px;
}
.btitle span { flex: 1; }
.btitle .open { height: 20px; }
dialog.bode form, .bode-pane { display: flex; flex-direction: column; gap: 6px; }
.bhead { display: flex; align-items: flex-start; gap: 24px; flex-wrap: wrap; }
.bfields {
    display: grid;
    grid-template-columns: 1.2em 12em 1.8em;
    gap: 4px 6px;
    align-items: center;
}
.bfields .pair { min-width: 0; }
.bsum { margin: 0 auto 0 0; }
.bsum dl { display: grid; grid-template-columns: auto auto; gap: 2px 10px; margin: 0; }
.bsum dt { color: var(--mute); }
.bsum dd { margin: 0; text-align: right; font-weight: 600; }
.bsw { width: auto; margin: 0; font-size: 12px; }
.bsw th, .bsw td { padding: 1px 8px 1px 0; }
.bsw thead th { text-align: right; }
.bsw thead th:first-child, .bsw tbody th { text-align: left; }
.bsw .sw { display: inline-block; width: 10px; height: 2px; margin: 0 5px 3px 0; vertical-align: middle; }
#bsweep { height: 24px; align-self: flex-start; }
#bsweep[aria-pressed="true"] { color: var(--bg); background: var(--fg); border-color: var(--fg); }
#bplot .sl { font-size: 10px; font-family: inherit; font-weight: 600; }
.bmsg { min-height: 1.35em; color: var(--accent); font-weight: 600; font-size: 12px; }
#bplot { flex: 1; min-height: 0; width: 100%; display: block; }
#bplot .g { stroke: var(--rule); stroke-width: 1; }
#bplot .ax { stroke: var(--mute); stroke-width: 1; fill: none; }
#bplot .t { fill: var(--mute); font-size: 11px; font-family: inherit; }
#bplot .l { fill: none; stroke: var(--accent); stroke-width: 1.8; }
#bplot .z { fill: none; stroke: var(--mute); stroke-width: 1; stroke-dasharray: 2 3; }
#bplot .mk { stroke: var(--fg); stroke-width: 1; stroke-dasharray: 4 3; }
#bplot .mt { fill: var(--fg); font-size: 11px; font-family: inherit; }
#bplot .ref { stroke: var(--mute); stroke-width: 1; stroke-dasharray: 5 4; }
#bplot .ref3 { stroke: var(--rule); stroke-width: 1; stroke-dasharray: 2 4; }
.seg2.gu { display: inline-flex; height: 20px; font-size: 11px; }
.seg2.gu span { padding: 0 5px; }

/* bode.html (pop-out) */
body.bodepage { height: 100vh; }
.bode-pane { height: 100vh; padding: 10px 14px; }
.bode-pane #bplot.stale, dialog.bode #bplot.stale { opacity: 0.35; }
#gain-unit { margin-left: 2px; }

/* uncommitted edit (recompute happens on Enter / leaving the field) */
input.dirty, textarea.dirty { color: var(--mute); border-bottom: 1px dashed var(--mute); }
#bexport { height: 24px; align-self: flex-start; }
.btitle #bexport { height: 20px; }

/* "?" help icon + popover (V_R : V_C) */
.lblhelp { position: relative; display: inline-flex; align-items: center; white-space: nowrap; }
.help {
    position: absolute; right: -10px; top: -7px;
    width: 13px; height: 13px; padding: 0;
    font-family: inherit; font-size: 9px; font-weight: 600; line-height: 11px; text-align: center;
    color: var(--mute); background: var(--bg);
    border: 1px solid var(--mute); border-radius: 50%; cursor: help;
}
.help:hover, .help:focus-visible, .help[aria-expanded="true"] { color: var(--accent); border-color: var(--accent); outline: none; }
.pop {
    position: fixed; z-index: 20;
    display: flex; gap: 14px; align-items: flex-start;
    width: min(640px, calc(100vw - 16px));
    padding: 12px 14px;
    background: var(--bg); color: var(--fg);
    border: 1px solid var(--fg);
}
.pop[hidden] { display: none; }
.pop figure { margin: 0; flex: none; width: 260px; }
.pop img { display: block; width: 100%; height: auto; }
.pop figcaption { font-size: 10.5px; color: var(--mute); margin-top: 4px; }
.pop blockquote {
    margin: 0; padding: 0 0 0 10px;
    border-left: 2px solid var(--rule);
    font-size: 12px; line-height: 1.6;
}
.pop blockquote p { margin: 0; }
.pop footer { margin-top: 6px; font-size: 10.5px; color: var(--mute); }
@media (max-width: 640px) { .pop { flex-direction: column; } .pop figure { width: 100%; } }

/* NPN / PNP toggle next to the mode switch */
.toprow { display: flex; align-items: center; gap: 10px; }
.seg2.pol { height: 26px; font-size: 11px; letter-spacing: 0.03em; }
textarea.wrongtype { border-color: var(--accent); }

/* current arrows over the schematic */
.curov { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; overflow: visible; }
.curov .curln { stroke: var(--accent); stroke-width: 1.6; fill: none; }
.curov .curhd { fill: var(--accent); }
.curov .curtx { fill: var(--accent); font: 600 12px "Segoe UI", system-ui, sans-serif; }
.curov .sub { font-size: 9px; }
#out .lblhelp { display: inline-block; }
#out .lblhelp .help { right: -14px; top: -4px; }

/* Bode dialog / pop-out: source selector, knobs, stability view */
.bsrc { display: inline-flex; align-items: center; gap: 6px; align-self: flex-start; }
.bsrc select { width: auto; height: 24px; text-align: left; text-align-last: left; padding: 0 22px 0 6px; }
.bknobs { display: inline-flex; gap: 6px; font-size: 12px; color: var(--mute); }
.bknobs[hidden] { display: none; }
.bknobs input { width: 3em; height: 24px; text-align: right; }
#bview { height: 24px; align-self: flex-start; font-size: 11px; }
.bstab { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 4px; }
.bstab[hidden] { display: none; }
.bstab.stale { opacity: 0.35; }
.bstab .zhead { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.bstab .zstate { font-weight: 600; font-size: 13px; }
.bstab .zval { font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; }
.bstab .zval span { font-size: 13px; font-weight: 400; color: var(--mute); }
.bstab .zrhp { font-size: 11px; color: var(--mute); }
.bstab .zhead.unstable .zstate, .bstab .zhead.unstable .zval,
.bstab .zhead.marginal .zstate, .bstab .zhead.marginal .zval { color: var(--accent); }
.bstab-plots { flex: 1; min-height: 0; display: flex; }
.bstab-plots .tplot { flex: 1; min-width: 0; height: 100%; }
.bstab svg .g { stroke: var(--rule); stroke-width: 1; }
.bstab svg .ax { stroke: var(--mute); stroke-width: 1; fill: none; }
.bstab svg .t { fill: var(--mute); font-size: 10px; font-family: inherit; }
.bstab svg .l { fill: none; stroke: var(--accent); stroke-width: 1.6; }
.bstab svg .ref { stroke: var(--mute); stroke-width: 1; stroke-dasharray: 5 4; fill: none; }
.bstab svg .mk { stroke: var(--fg); stroke-width: 1; stroke-dasharray: 4 3; }
.bstab svg .mt { fill: var(--fg); font-size: 10px; font-family: inherit; }

/* topology: grid triplets shown only for one topology */
.tg { display: contents; }
body.classic .tg.anth, body:not(.classic) .tg.cls { display: none; }
body.classic .schematic .layer { display: none; }
