:root {
  --radius: 8px;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  font-size: 14px;
  color: var(--ink);
}

/* ---------- Themes: light (default), gray, dark ---------- */
:root, :root[data-theme="light"] {
  color-scheme: light;
  --bg: #e9edf2; --surface: #ffffff; --surface-2: #f5f7fa; --panel: #f4f6f9; --field: #ffffff; --inst: #f3f5f8;
  --ink: #1c2633; --ink-2: #3b4757; --muted: #687586; --line: #d6dce4; --line-2: #c4ccd6;
  --accent: #1f6feb; --accent-soft: #e3edff; --danger: #d1352b; --danger-soft: #fff2f1; --danger-line: #f5c2bd;
  --warn: #a8660b; --good: #1f9d55; --good-soft: #e7f7ee; --good-line: #9fd9b8; --good-ink: #126b3a;
  --ai-soft: #f6f2ff; --ai-soft-2: #ece4ff; --ai-line: #d8cdfa; --ai-ink: #4a32b0;
  --nl-fill: #eef4ff; --nl-line: #5b7fc7; --nl-ink: #2c4f97; --cha: #9a7500; --chb: #0b7ea3;
  --grid: #b9c3cf; --canvas: #fbfcfd; --bulb: rgba(255,255,255,.6); --code-bg: rgba(0,0,0,.06);
}
:root[data-theme="gray"] {
  color-scheme: light;
  --bg: #b9bec5; --surface: #d3d7dc; --surface-2: #c6cbd1; --panel: #cfd3d8; --field: #e4e7ea; --inst: #dfe2e6;
  --ink: #171d25; --ink-2: #2d3640; --muted: #4f5966; --line: #a8afb8; --line-2: #929aa5;
  --accent: #1a5fd0; --accent-soft: #c3d5f3; --danger: #b52a21; --danger-soft: #edd3d0; --danger-line: #d49b95;
  --warn: #8a5300; --good: #17804a; --good-soft: #c5e0d0; --good-line: #7fb898; --good-ink: #0e5a30;
  --ai-soft: #d9d2ee; --ai-soft-2: #ccc1ec; --ai-line: #a998e0; --ai-ink: #3e2a9c;
  --nl-fill: #d5e0f3; --nl-line: #4e6fb0; --nl-ink: #22437f; --cha: #7a5c00; --chb: #075f7c;
  --grid: #9aa2ac; --canvas: #cdd1d6; --bulb: rgba(255,255,255,.45); --code-bg: rgba(0,0,0,.08);
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #12161b; --surface: #1c2128; --surface-2: #262c35; --panel: #1f252c; --field: #14181d; --inst: #2a313b;
  --ink: #dde3ea; --ink-2: #b4bdc8; --muted: #8b96a4; --line: #333b46; --line-2: #45505d;
  --accent: #5b9bff; --accent-soft: #1f3354; --danger: #ff6b61; --danger-soft: #3a1f1d; --danger-line: #6e2c27;
  --warn: #e3a64c; --good: #3ccf7f; --good-soft: #17301f; --good-line: #2b6a45; --good-ink: #74e0a2;
  --ai-soft: #231e38; --ai-soft-2: #2e2654; --ai-line: #4b3f8a; --ai-ink: #c9bbff;
  --nl-fill: #1d2c47; --nl-line: #5b7fc7; --nl-ink: #a8c3ff; --cha: #e6c84c; --chb: #4dd2ff;
  --grid: #2e3742; --canvas: #161a1f; --bulb: rgba(255,255,255,.08); --code-bg: rgba(255,255,255,.08);
}
body, #palette, #props, .topbar, .statusbar, #ai-drawer, .panel { transition: background-color .15s, color .15s; }

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { display: flex; flex-direction: column; background: var(--bg); overflow: hidden; }
button, select, input { font: inherit; color: inherit; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap; row-gap: 6px;
  padding: 8px 14px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; margin-right: 6px; }
.brand b { display: block; font-size: 16px; letter-spacing: .2px; }
.brand small { display: block; color: var(--muted); font-size: 11px; margin-top: -2px; }
.logo { width: 32px; height: 32px; display: block; }
.tb-group { display: flex; align-items: center; gap: 4px; padding-left: 10px; border-left: 1px solid var(--line); }
.topbar button { white-space: nowrap; }
#sel-examples { max-width: 170px; }
.prefs select { padding: 0 6px; }
@media (max-width: 1500px) { .brand small { display: none; } }
.tb-spacer { flex: 1; }
.topbar button, .topbar select {
  height: 32px; padding: 0 12px; border: 1px solid var(--line); border-radius: 6px;
  background: var(--surface); cursor: pointer;
}
.topbar button:hover:not(:disabled), .topbar select:hover { background: var(--surface-2); border-color: var(--line-2); }
.topbar button:disabled { opacity: .4; cursor: default; }
button.icon { width: 34px; padding: 0; display: inline-grid; place-items: center; }
button.icon svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
button.icon.sm { width: 28px; height: 28px; font-size: 16px; }
.speed .lbl { color: var(--muted); margin-right: 4px; }
#spd-val { min-width: 42px; text-align: center; font-variant-numeric: tabular-nums; }

.power {
  display: flex; align-items: center; gap: 8px; height: 36px !important; padding: 0 18px 0 14px !important;
  border-radius: 18px !important; font-weight: 600; background: var(--surface-2) !important;
}
.pw-led { width: 12px; height: 12px; border-radius: 50%; background: var(--muted); box-shadow: inset 0 -1px 2px rgba(0,0,0,.25); }
.power.on { background: var(--good-soft) !important; border-color: var(--good-line) !important; color: var(--good-ink); }
.power.on .pw-led { background: #22c55e; box-shadow: 0 0 8px #22c55e; }

/* ---------- Layout ---------- */
main { flex: 1; display: flex; min-height: 0; }
#palette, #props { background: var(--surface); overflow-y: auto; }
#palette { width: 204px; border-right: 1px solid var(--line); padding-bottom: 16px; }
#props { width: 268px; border-left: 1px solid var(--line); }
#stage { position: relative; flex: 1; min-width: 0; overflow: hidden; background: var(--canvas); }
#canvas { width: 100%; height: 100%; display: block; touch-action: none; user-select: none; cursor: grab; }
#canvas.panning { cursor: grabbing; }
#canvas.placing { cursor: copy; }

.statusbar {
  display: flex; justify-content: space-between; gap: 16px; padding: 5px 14px;
  background: var(--surface); border-top: 1px solid var(--line); font-size: 12.5px; color: var(--muted);
}
#status-msg.warn { color: var(--warn); }
#status-msg.error { color: var(--danger); font-weight: 600; }
#status-time { font-variant-numeric: tabular-nums; white-space: nowrap; }

/* ---------- Palette ---------- */
.pal-head, .pp-head { padding: 12px 14px 8px; font-weight: 600; font-size: 13px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); }
.pal-cat { padding: 10px 14px 4px; font-size: 12px; color: var(--muted); }
.pal-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; padding: 0 10px; }
.pal-item {
  display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 7px 4px 6px;
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); cursor: grab; touch-action: none;
}
.pal-item:hover { border-color: var(--accent); background: var(--accent-soft); }
.pal-item span { font-size: 11.5px; line-height: 1.15; text-align: center; }
.pal-icon { width: 64px; height: 34px; overflow: visible; }
.pal-icon .glow { opacity: 0 !important; }

/* ---------- Schematic ---------- */
.griddot { fill: var(--grid); }
.bg { cursor: inherit; }
.part path, .part line, .part circle, .part rect:not(.hit) {
  stroke: var(--ink); stroke-width: 2; fill: none; stroke-linecap: round; stroke-linejoin: round;
}
.part .thick { stroke-width: 3.5; }
.part .solid { fill: var(--ink); }
.part .hit { fill: transparent; stroke: none; }
.part { cursor: move; }
.part text { font: 11px "Segoe UI", system-ui, sans-serif; fill: var(--ink); stroke: none; text-anchor: middle; pointer-events: none; }
.part .pol { font-size: 13px; font-weight: 700; text-anchor: start; }
.part .tiny { font-size: 9.5px; font-weight: 600; fill: var(--ink-2); }
.part .glow { stroke: none !important; opacity: 0; filter: blur(4px); }
.part .lamp-glow { fill: #ffc93c; }
.part .led-body { stroke: var(--ink); }
.part .arrows { stroke-width: 1.5; }
.part .contact { fill: var(--surface) !important; }
.part .bulb { fill: var(--bulb) !important; }
.part.sel path, .part.sel line, .part.sel circle:not(.tdot):not(.thit):not(.glow), .part.sel rect.inst { stroke: var(--accent); }
.part.sel .solid { fill: var(--accent); }

.part .inst { fill: var(--inst) !important; }
.part .lcd { fill: #c8dcba !important; stroke: #8fa585 !important; stroke-width: 1 !important; }
.part .lcdtext { font: 600 10px Consolas, "Courier New", monospace; fill: #142411; text-anchor: end; }
.part .instlbl { font-size: 9.5px; font-weight: 600; letter-spacing: .5px; fill: var(--muted); }
.part .jack { fill: #2b3542 !important; stroke: #2b3542 !important; }
.part .jack.plus { fill: #d63c3c !important; stroke: #a52a2a !important; }
.part .screen { fill: #0d2219 !important; stroke: #0d2219 !important; }
.part .trace { stroke: #7ef0a8 !important; stroke-width: 1.5 !important; }
.part .knob { fill: var(--line) !important; stroke-width: 1.5 !important; }
.part .wbtn { fill: var(--surface-2) !important; stroke: var(--line-2) !important; stroke-width: 1 !important; }
.part .wbtn.on { fill: #d8f3e3 !important; stroke: var(--good) !important; }
.part .wico { stroke-width: 1.5 !important; }

.part .pin { font-size: 10px; font-weight: 600; }
.part.hi .olead { stroke: #e5484d !important; stroke-width: 2.5; }
.part .lsw { fill: var(--line) !important; stroke: var(--muted) !important; stroke-width: 1.2 !important; }
.part .lsw.on { fill: #34c759 !important; stroke: #1f9d55 !important; }
.part .lknob { fill: #fff !important; stroke: var(--muted) !important; stroke-width: 1.2 !important; }
.part .probe { fill: var(--inst) !important; }
.part.hi .probe { fill: #ff4d3d !important; stroke: #b42318 !important; }
.part .probe-glow { fill: #ff4d3d; }
.part .hexbody { fill: #1b1f24 !important; stroke: #1b1f24 !important; }
.part .seg { stroke: #3b2524 !important; stroke-width: 4 !important; }
.part .seg.on { stroke: #ff4d3d !important; }
.part .chiptitle { font-size: 11px; font-weight: 700; fill: var(--muted); letter-spacing: .5px; }
.part .fbreak { display: none; }
.part.blown .fwire { display: none; }
.part.blown .fbreak { display: inline; stroke: var(--danger) !important; }
.part .sound { opacity: 0; stroke-width: 1.6 !important; }
.part.on .sound { opacity: 1; stroke: #e5484d !important; animation: buzz .18s infinite alternate; }
@keyframes buzz { from { transform: translateX(0); } to { transform: translateX(2px); } }
.part .dash { stroke-dasharray: 3 3; stroke-width: 1.4 !important; }
.part .rgb-glow { fill: #fff; }
.part .netlabel { fill: var(--nl-fill) !important; stroke: var(--nl-line) !important; stroke-width: 1.4 !important; }
.part .nltext { font-size: 10.5px; font-weight: 600; fill: var(--nl-ink); }
.part .hexpin { font-size: 8px; fill: #8ea0b3; text-anchor: start; }

.tdot { fill: var(--surface) !important; stroke: var(--ink); stroke-width: 1.4 !important; opacity: 0; pointer-events: none; }
.part:hover .tdot { opacity: 1; }
.thit { fill: transparent !important; stroke: none !important; cursor: crosshair; }
.thit:hover { fill: rgba(31,111,235,.18) !important; }
.plabel, .pvalue { font: 600 11.5px "Segoe UI", system-ui, sans-serif; fill: var(--ink); cursor: move; user-select: none; }
.pvalue { font-weight: 400; fill: var(--muted); }

.wire-line { stroke: var(--ink); stroke-width: 2; stroke-linecap: round; pointer-events: none; }
.wire-hit { stroke: transparent; stroke-width: 12; stroke-linecap: round; cursor: pointer; }
.wire:hover .wire-line { stroke: var(--ink-2); stroke-width: 3; }
.wire.sel .wire-line { stroke: var(--accent); stroke-width: 3; }
.junction { fill: var(--ink); pointer-events: none; }
.wire-preview { stroke: var(--accent); stroke-width: 2; stroke-dasharray: 5 4; pointer-events: none; }
.wire-end { fill: var(--accent); pointer-events: none; }
.ghost { opacity: .45; pointer-events: none; }

.zoom { position: absolute; left: 12px; bottom: 12px; display: flex; flex-direction: column; gap: 4px; z-index: 5; }
.zoom button {
  width: 32px; height: 32px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface);
  cursor: pointer; font-size: 16px; box-shadow: 0 1px 2px var(--code-bg);
}
.zoom button:hover { background: var(--surface-2); }

#tooltip {
  position: absolute; z-index: 9999; pointer-events: none; background: rgba(20,28,38,.92); color: #fff;
  padding: 5px 9px; border-radius: 6px; font-size: 12.5px; white-space: nowrap; font-variant-numeric: tabular-nums;
}

/* ---------- Properties ---------- */
.pp-body { padding: 0 14px 16px; }
.pp-body h4 { margin: 14px 0 6px; font-size: 13px; }
.muted { color: var(--muted); }
.small { font-size: 12px; line-height: 1.45; }
.tips { margin: 0; padding-left: 18px; color: var(--ink-2); font-size: 12.5px; line-height: 1.5; }
.tips li { margin-bottom: 5px; }
kbd { font: 11px Consolas, monospace; padding: 1px 5px; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; background: var(--surface-2); }
.pp-row { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; font-size: 12.5px; color: var(--ink-2); }
.pp-row.check { flex-direction: row; align-items: center; gap: 8px; }
.pp-row input[type=text], .pp-row select { height: 32px; padding: 0 9px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); width: 100%; }
.pp-row input[type=text]:focus, .pp-row select:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.pp-row input[type=range] { width: 100%; }
.pp-in { display: flex; align-items: center; gap: 6px; }
.pp-in em { font-style: normal; color: var(--muted); min-width: 18px; }
input.bad { border-color: var(--danger) !important; background: var(--danger-soft) !important; }
.pp-live { min-height: 18px; margin: 4px 0 12px; font-size: 12.5px; color: var(--good-ink); font-variant-numeric: tabular-nums; }
.pp-live:empty { display: none; }
.pp-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.pp-actions button { height: 32px; padding: 0 12px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); cursor: pointer; }
.pp-actions button:hover { background: var(--surface-2); }
.pp-actions .primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.pp-actions .primary:hover { background: #1a5fd0; }
.pp-actions .danger { color: var(--danger); }

/* ---------- Instrument panels ---------- */
#panels { position: absolute; inset: 0; pointer-events: none; }
.panel {
  position: absolute; pointer-events: auto; background: var(--panel); border: 1px solid var(--line-2); border-radius: 10px;
  box-shadow: 0 10px 30px rgba(20,30,45,.18), 0 2px 6px rgba(20,30,45,.08); overflow: hidden;
}
.ph { display: flex; align-items: center; justify-content: space-between; padding: 6px 6px 6px 12px; background: #27313d; color: #e9eef4; cursor: move; user-select: none; touch-action: none; }
.pt { font-size: 12.5px; font-weight: 600; }
.px { width: 24px; height: 24px; border: 0; border-radius: 5px; background: transparent; color: #c5ced9; font-size: 17px; line-height: 1; cursor: pointer; }
.px:hover { background: rgba(255,255,255,.12); color: #fff; }
.pb { padding: 12px; }
.hint { margin: 10px 0 0; font-size: 12px; color: var(--muted); line-height: 1.4; }

.seg { display: flex; gap: 4px; margin-top: 8px; }
.seg button {
  flex: 1; height: 34px; border: 1px solid var(--line-2); border-radius: 6px; background: var(--field); cursor: pointer; font-weight: 600;
}
.seg button:hover:not(:disabled) { background: var(--surface-2); }
.seg button.on { background: #27313d; border-color: #27313d; color: #fff; }
.seg button:disabled { opacity: .4; cursor: default; }

.panel-mm { width: 250px; }
.mm-lcd {
  height: 62px; display: flex; align-items: center; justify-content: flex-end; padding: 0 14px;
  background: linear-gradient(#cde0bf, #bcd2ad); border: 2px solid #8fa585; border-radius: 6px;
  font: 600 27px Consolas, "Courier New", monospace; color: #13240e; font-variant-numeric: tabular-nums;
}

.panel-scope { width: 520px; }
.scope-screen { width: 100%; height: 300px; display: block; border-radius: 6px; border: 3px solid #1f2a35; }
.scope-read { display: flex; justify-content: space-between; gap: 8px; margin: 6px 2px 2px; font-size: 12px; font-variant-numeric: tabular-nums; }
.scope-read .ra { color: var(--cha); }
.scope-read .rb { color: var(--chb); }
.scope-ctl { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 8px; }
.scope-ctl fieldset { margin: 0; padding: 6px 8px 8px; border: 1px solid var(--line-2); border-radius: 6px; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; font-size: 12px; }
.scope-ctl legend { padding: 0 4px; font-weight: 600; font-size: 11.5px; color: var(--ink-2); }
.scope-ctl .cha legend { color: var(--cha); }
.scope-ctl .chb legend { color: var(--chb); }
.scope-ctl select, .scope-ctl input { height: 26px; border: 1px solid var(--line-2); border-radius: 5px; background: var(--field); padding: 0 4px; font-size: 12px; }
.scope-ctl input[type=number] { width: 58px; }
.scope-ctl input[type=text] { width: 54px; }
.scope-ctl label { display: flex; align-items: center; gap: 4px; }

.panel-fgen { width: 270px; }
.waves { margin-top: 0; }
.waves svg { width: 30px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linejoin: round; stroke-linecap: round; }
.fg-grid { display: grid; grid-template-columns: auto 1fr auto; gap: 8px 8px; align-items: center; margin-top: 12px; font-size: 12.5px; }
.fg-grid input { height: 30px; border: 1px solid var(--line-2); border-radius: 6px; padding: 0 8px; width: 100%; background: var(--field); }
.fg-grid input:disabled { opacity: .45; }
.fg-grid em { font-style: normal; color: var(--muted); font-size: 12px; }

/* ---------- AI assistant ---------- */
.topbar .ai-btn { font-weight: 600; color: #5b3fd1; border-color: var(--ai-line); background: var(--ai-soft); }
.topbar .ai-btn:hover { background: var(--ai-soft-2) !important; }
.topbar .ai-btn.on { background: #5b3fd1 !important; color: #fff; border-color: #5b3fd1 !important; }
#ai-drawer {
  position: absolute; top: 0; right: 0; bottom: 0; width: 390px; max-width: 100%; z-index: 9000;
  display: flex; flex-direction: column; background: var(--surface); border-left: 1px solid var(--line);
  box-shadow: -8px 0 24px rgba(20,30,45,.10);
}
#ai-drawer[hidden] { display: none; }
.ai-head { display: flex; align-items: center; gap: 4px; padding: 10px 10px 6px 14px; }
.ai-head b { flex: 1; font-size: 14px; }
.ai-icon { width: 28px; height: 28px; border: 0; border-radius: 6px; background: transparent; cursor: pointer; font-size: 15px; color: var(--muted); }
.ai-icon:hover { background: var(--surface-2); color: var(--ink); }
.ai-models { display: flex; gap: 6px; padding: 0 14px 8px; }
.ai-models select { height: 30px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); padding: 0 6px; min-width: 0; }
.ai-provider { width: 96px; }
.ai-model { flex: 1; }
.ai-small, .ai-row button { height: 30px; padding: 0 10px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); cursor: pointer; font-size: 12.5px; white-space: nowrap; }
.ai-small:hover, .ai-row button:hover { background: var(--surface-2); }
.ai-row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.ai-row .primary, .ai-send.primary { background: #5b3fd1; border-color: #5b3fd1; color: #fff; }
.ai-row .danger { color: var(--danger); }
.ai-keys { margin: 0 14px 8px; padding: 10px 12px; border: 1px solid var(--ai-line); border-radius: 8px; background: var(--ai-soft); font-size: 12.5px; }
.ai-keys p { margin: 0 0 8px; line-height: 1.45; color: var(--ink-2); }
.ai-keys label { display: flex; flex-direction: column; gap: 3px; margin-bottom: 8px; color: var(--ink-2); }
.ai-keys input[type=password] { height: 30px; border: 1px solid var(--line); border-radius: 6px; padding: 0 8px; font: 12px Consolas, monospace; }
.ai-small-note { font-size: 11.5px; color: var(--muted); }
.ai-quick { display: flex; gap: 6px; padding: 0 14px 8px; flex-wrap: wrap; }
.ai-quick button { height: 28px; padding: 0 10px; border: 1px solid var(--ai-line); border-radius: 14px; background: var(--ai-soft); color: var(--ai-ink); cursor: pointer; font-size: 12px; }
.ai-quick button:hover { background: var(--ai-soft-2); }
.ai-log { flex: 1; overflow-y: auto; padding: 6px 14px 10px; display: flex; flex-direction: column; gap: 8px; border-top: 1px solid var(--line); }
.ai-msg { padding: 8px 11px; border-radius: 10px; font-size: 13px; line-height: 1.5; max-width: 100%; overflow-wrap: anywhere; }
.ai-msg p { margin: 0 0 6px; }
.ai-msg p:last-child { margin-bottom: 0; }
.ai-msg ul { margin: 2px 0 6px; padding-left: 18px; }
.ai-msg code { font: 12px Consolas, monospace; background: var(--code-bg); padding: 0 3px; border-radius: 3px; }
.ai-user { align-self: flex-end; background: #5b3fd1; color: #fff; max-width: 85%; }
.ai-bot { background: var(--surface-2); border: 1px solid var(--line); }
.ai-note { background: transparent; color: var(--muted); font-size: 12px; padding: 2px 0; }
.ai-error { background: var(--danger-soft); color: var(--danger); border: 1px solid var(--danger-line); }
.ai-h { font-weight: 700; }
.ai-working { color: var(--muted); font-style: italic; }
.ai-warn { color: var(--warn); font-size: 12px; }
.ai-warn summary { cursor: pointer; }
.ai-notes { margin: 6px 0; padding: 6px 9px; border-left: 3px solid var(--ai-line); background: var(--ai-soft); border-radius: 0 6px 6px 0; }
.ai-meta { font-size: 12px; color: var(--good-ink); }
.ai-compose { padding: 8px 14px 12px; border-top: 1px solid var(--line); }
.ai-modes { margin: 0 0 6px; }
.ai-modes button { height: 28px; font-size: 12.5px; }
.ai-modes button.on { background: #5b3fd1; border-color: #5b3fd1; }
.ai-input { width: 100%; resize: vertical; min-height: 58px; border: 1px solid var(--line); border-radius: 8px; padding: 8px; font: inherit; font-size: 13px; }
.ai-input:focus { outline: 2px solid var(--ai-soft-2); border-color: #5b3fd1; }
.ai-send-row { display: flex; align-items: center; justify-content: space-between; margin-top: 6px; }
.ai-hint { font-size: 11px; color: var(--muted); }
.ai-send { height: 30px; padding: 0 16px; border: 1px solid var(--line); border-radius: 6px; cursor: pointer; font-weight: 600; }
.ai-send.danger { background: var(--danger) !important; border-color: var(--danger) !important; }

@media (max-width: 900px) {
  #props { display: none; }
  #palette { width: 160px; }
  .brand small, .speed .lbl { display: none; }
}

/* ---------- Formula boxes ---------- */
.part .fx-fo { pointer-events: none; overflow: visible; }
.fx-box {
  display: inline-block; pointer-events: auto; max-width: 540px; padding: 7px 11px 8px; border-radius: 8px;
  background: var(--surface); border: 1.5px solid var(--nl-line); box-shadow: 0 1px 3px rgba(0,0,0,.08);
  font: 13px "Segoe UI", system-ui, sans-serif; color: var(--ink); line-height: 1.35; cursor: move;
}
.part.sel .fx-box { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.fx-cap { font-size: 11.5px; font-weight: 600; color: var(--nl-ink); margin-bottom: 2px; }
.fx-l1 { font-size: 15px; }
.fx-l2 { font-size: 13.5px; color: var(--ink-2); margin-top: 2px; }
.fx-live { font-size: 12px; margin-top: 4px; color: var(--ink-2); }
.fx-live:empty { display: none; }
.fx-src { font-family: Consolas, monospace; font-size: 11.5px; color: var(--muted); }
.fx-ok { color: var(--good); font-weight: 700; }
.fx-diff { color: var(--warn); font-weight: 600; }
.fx-bad { color: var(--danger); font-size: 12px; }
.fx-muted { color: var(--muted); font-style: italic; }
.fx-box code { font: 12.5px Consolas, monospace; }
.fx-box .katex { font-size: 1.05em; }
.part .fx-icon { fill: var(--nl-fill) !important; stroke: var(--nl-line) !important; stroke-width: 1.4 !important; }
.part .fx-icontext { font: italic 600 12px "Times New Roman", serif; fill: var(--nl-ink); }
.ai-msg .katex-display { margin: 6px 0; overflow-x: auto; overflow-y: hidden; }

/* ---------- Scope math channel ---------- */
.scope-read { display: grid; grid-template-columns: 1fr; gap: 1px; }
.scope-read .rm { color: #c2399c; }
.scope-read .rp { color: var(--ink-2); font-weight: 600; }
.scope-read div:empty { display: none; }
.scope-ctl .chm { grid-column: 1 / -1; }
.scope-ctl .chm legend { color: #c2399c; }
.scope-ctl input[data-k="mExpr"] { width: 120px; font-family: Consolas, monospace; }
:root[data-theme="dark"] .scope-read .rm, :root[data-theme="dark"] .scope-ctl .chm legend { color: #ff7ad9; }

/* ---------- Calculators ---------- */
.panel-calc { width: 430px; }
.calc-tabs { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 10px; }
.calc-tabs button { height: 28px; padding: 0 9px; border: 1px solid var(--line-2); border-radius: 14px; background: var(--field); cursor: pointer; font-size: 12px; }
.calc-tabs button.on { background: #27313d; border-color: #27313d; color: #fff; }
.calc-note { margin: 0 0 8px; font-size: 12px; color: var(--muted); line-height: 1.4; }
.calc-grid { display: grid; grid-template-columns: auto 1fr auto; gap: 6px 8px; align-items: center; font-size: 12.5px; }
.calc-grid input, .calc-grid select { height: 28px; border: 1px solid var(--line-2); border-radius: 6px; padding: 0 7px; background: var(--field); min-width: 0; }
.calc-grid em { font-style: normal; color: var(--muted); font-size: 12px; }
.calc-out { margin-top: 10px; border-top: 1px solid var(--line); padding-top: 8px; display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; font-size: 13px; }
.calc-out b { font-variant-numeric: tabular-nums; }
.calc-out .calc-k { color: var(--ink-2); }
.calc-err { color: var(--danger); font-size: 12px; margin-top: 8px; }
.calc-bands { display: flex; justify-content: center; margin: 4px 0 8px; }
.calc-swatch { display: inline-block; width: 12px; height: 12px; border-radius: 3px; border: 1px solid rgba(0,0,0,.25); vertical-align: -2px; margin-right: 4px; }

/* ---------- Tutorials ---------- */
.topbar .tut-btn { font-weight: 600; color: var(--good-ink); border-color: var(--good-line); background: var(--good-soft); }
.topbar .tut-btn:hover { filter: brightness(.97); }
.topbar .tut-btn.on { background: var(--good); border-color: var(--good); color: #fff; }
.tut-coach { left: 16px; bottom: 56px; width: 340px; z-index: 9600; }
.tut-intro { margin: 0 0 8px; font-size: 12.5px; color: var(--ink-2); line-height: 1.45; }
.tut-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; max-height: 50vh; overflow-y: auto; }
.tut-list button { width: 100%; display: flex; align-items: center; gap: 10px; padding: 7px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--field); cursor: pointer; text-align: left; font-size: 13px; }
.tut-list button:hover { border-color: var(--good-line); background: var(--good-soft); }
.tut-num, .tut-done { display: inline-grid; place-items: center; width: 22px; height: 22px; border-radius: 50%; font-size: 12px; font-weight: 700; flex: none; }
.tut-num { background: var(--surface-2); color: var(--ink-2); border: 1px solid var(--line-2); }
.tut-done { background: var(--good); color: #fff; }
.tut-head { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.tut-head b { flex: 1; }
.tut-head span { color: var(--muted); font-size: 12px; white-space: nowrap; }
.tut-back-list { width: 26px; height: 26px; border: 1px solid var(--line); border-radius: 6px; background: var(--field); cursor: pointer; }
.tut-bar { height: 4px; border-radius: 2px; background: var(--surface-2); margin: 8px 0 10px; overflow: hidden; }
.tut-bar div { height: 100%; background: var(--good); transition: width .3s; }
.tut-text { font-size: 13.5px; line-height: 1.55; }
.tut-text code { font: 12.5px Consolas, monospace; background: var(--code-bg); padding: 1px 4px; border-radius: 4px; }
.tut-status { min-height: 20px; margin-top: 8px; font-size: 12.5px; }
.tut-ok { color: var(--good); font-weight: 700; }
.tut-wait { color: var(--muted); font-style: italic; }
.tut-row { display: flex; gap: 6px; justify-content: flex-end; margin-top: 6px; }
.tut-row button { height: 30px; padding: 0 12px; border: 1px solid var(--line); border-radius: 6px; background: var(--field); cursor: pointer; }
.tut-row button:disabled { opacity: .45; cursor: default; }
.tut-row .primary { background: var(--good); border-color: var(--good); color: #fff; font-weight: 600; }
.tut-ring {
  position: fixed; z-index: 99999; pointer-events: none; border: 3px solid #22c55e; border-radius: 10px;
  box-shadow: 0 0 0 4px rgba(34,197,94,.25), 0 0 18px rgba(34,197,94,.55); animation: tutpulse 1.4s ease-in-out infinite;
  transition: left .2s, top .2s, width .2s, height .2s;
}
.tut-ring[hidden] { display: none; }
@keyframes tutpulse { 50% { box-shadow: 0 0 0 8px rgba(34,197,94,.12), 0 0 26px rgba(34,197,94,.7); } }

/* Bode plotter */
.panel-bode { width: 560px; }
.bode-screen { width: 100%; height: 340px; display: block; border-radius: 6px; border: 3px solid #1f2a35; cursor: crosshair; touch-action: none; }
.bode-read { display: grid; gap: 1px; margin: 6px 2px 2px; font-size: 12px; font-variant-numeric: tabular-nums; min-height: 32px; }
.bode-read .bc { color: var(--ink); font-weight: 600; }
.bode-read .bs { color: var(--ink-2); }
.panel-bode .scope-ctl input:not([type]) { width: 64px; }

/* Scope spectrum view */
.scope-ctl .chv select { min-width: 0; }
.scope-ctl .trg { grid-column: 1 / -1; }
.scope-ctl .chv select[data-k="fftSpan"] { max-width: 130px; }

/* Fault finding */
.panel-fault { width: 340px; }
.panel-fault .pb { display: grid; gap: 8px; font-size: 12.5px; }
.flt-status { margin: 0; line-height: 1.45; }
.flt-guess { font-weight: 600; color: var(--accent); }
.flt-actions { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.panel-fault button { height: 28px; padding: 0 10px; border: 1px solid var(--line-2); border-radius: 6px; background: var(--field); cursor: pointer; font-size: 12.5px; color: var(--ink); }
.panel-fault button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.panel-fault button:disabled { opacity: 0.5; cursor: default; }
.panel-fault select { height: 28px; border: 1px solid var(--line-2); border-radius: 6px; background: var(--field); color: var(--ink); font-size: 12px; }
.flt-att { color: var(--ink-2); }
.flt-msg { margin: 0; padding: 7px 9px; border-radius: 6px; line-height: 1.45; }
.flt-msg.ok { background: rgba(46, 160, 67, 0.13); border: 1px solid rgba(46, 160, 67, 0.4); }
.flt-msg.bad { background: rgba(229, 57, 53, 0.1); border: 1px solid rgba(229, 57, 53, 0.35); }
.flt-row { display: flex; justify-content: space-between; align-items: center; gap: 8px; font-weight: 600; }
.flt-teach { border-top: 1px solid var(--line-2); padding-top: 6px; }
.flt-teach summary { cursor: pointer; font-weight: 600; color: var(--ink-2); margin-bottom: 6px; }
.flt-teach > * + * { margin-top: 6px; }
.flt-hint { color: var(--ink-2); }
.flt-check { display: flex; align-items: center; gap: 6px; }
.flt-ans { color: #d32f2f; font-weight: 600; }
.part.faulty { filter: drop-shadow(0 0 3px #e53935) drop-shadow(0 0 2px #e53935); }
#canvas.guessing, #canvas.guessing .part { cursor: help; }
#canvas.guessing .part:hover { filter: drop-shadow(0 0 4px var(--accent)); }

/* Word generator */
.panel-wordgen { width: 400px; }
.wg-ctl { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-bottom: 6px; font-size: 12px; }
.wg-ctl label { display: flex; align-items: center; gap: 4px; }
.wg-ctl input, .wg-ctl select { height: 26px; border: 1px solid var(--line-2); border-radius: 5px; background: var(--field); color: var(--ink); padding: 0 4px; font-size: 12px; }
.wg-ctl input { width: 52px; }
.wg-ctl button, .lc-row button, .lc-side button { height: 26px; padding: 0 8px; border: 1px solid var(--line-2); border-radius: 5px; background: var(--field); color: var(--ink); cursor: pointer; font-size: 12px; }
.wg-ctl button:disabled { opacity: 0.45; cursor: default; }
.wg-lbl { color: var(--ink-2); }
.wg-wrap { max-height: 300px; overflow: auto; border: 1px solid var(--line-2); border-radius: 6px; }
.wg-table { border-collapse: collapse; width: 100%; font: 12px Consolas, monospace; }
.wg-table th { position: sticky; top: 0; background: var(--surface); font-weight: 600; color: var(--ink-2); padding: 3px 0; }
.wg-table td { text-align: center; padding: 1px 0; border-top: 1px solid var(--line-2); }
.wg-table td input { width: 30px; height: 20px; text-align: center; font: 12px Consolas, monospace; border: 1px solid var(--line-2); border-radius: 4px; background: var(--field); color: var(--ink); }
.wg-table td.bit { cursor: pointer; color: var(--ink-2); user-select: none; }
.wg-table td.bit.on { color: #fff; background: #2e7d32; font-weight: 700; }
.wg-table tr.cur td { background: rgba(255, 216, 77, 0.22); }
.wg-table tr.cur td.bit.on { background: #f9a825; }

/* Logic analyzer */
.panel-logican { width: 540px; }
.la-screen { width: 100%; height: 300px; display: block; border-radius: 6px; border: 3px solid #1f2a35; cursor: crosshair; }
.la-read { display: grid; gap: 1px; margin: 6px 2px 2px; font: 12px Consolas, monospace; min-height: 30px; }
.la-read .lc { color: var(--ink-2); }

/* Logic converter */
.panel-logicconv { width: 520px; }
.lc-row { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; font-size: 12px; margin-bottom: 8px; }
.lc-row select, .lc-side input { height: 26px; border: 1px solid var(--line-2); border-radius: 5px; background: var(--field); color: var(--ink); padding: 0 4px; font-size: 12px; }
.lc-main { display: grid; grid-template-columns: auto 1fr; gap: 12px; }
.lc-twrap { max-height: 360px; overflow: auto; border: 1px solid var(--line-2); border-radius: 6px; }
.lc-table { border-collapse: collapse; font: 12px Consolas, monospace; }
.lc-table th { position: sticky; top: 0; background: var(--surface); padding: 3px 7px; color: var(--ink-2); }
.lc-table td { text-align: center; padding: 1px 7px; border-top: 1px solid var(--line-2); }
.lc-table .lc-out { border-left: 1px solid var(--line-2); font-weight: 700; }
.lc-table td.lc-out { cursor: pointer; user-select: none; }
.lc-table td.v1 { color: #fff; background: #2e7d32; }
.lc-table td.vx { color: #fff; background: #8e8e8e; }
.lc-side { display: flex; flex-direction: column; gap: 6px; font-size: 12px; min-width: 0; }
.lc-side input { font-family: Consolas, monospace; font-size: 13px; }
.lc-side button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.lc-side .hint { margin: 0; }
.lc-lbl { font-weight: 600; color: var(--ink-2); font-size: 11.5px; }
.lc-simple { font-size: 15px; overflow-x: auto; }
.lc-mint { font-family: Consolas, monospace; word-break: break-word; }
.lc-plain code { font-size: 12px; }
.lc-msg { margin: 8px 0 0; font-size: 12px; min-height: 16px; color: var(--ink-2); }
.lc-msg.bad { color: var(--danger); }

/* AI suggestions */
.ai-sugg { margin: 0 0 6px; }
.ai-sugg-head { display: flex; align-items: center; justify-content: space-between; gap: 6px; margin-bottom: 4px; }
.ai-sugg-toggle, .ai-sugg-more { border: 0; background: none; color: var(--ink-2); font-size: 12px; cursor: pointer; padding: 2px 0; }
.ai-sugg-toggle { font-weight: 600; }
.ai-sugg-more:hover, .ai-sugg-toggle:hover { color: var(--ai-ink); }
.ai-sugg-list { display: grid; gap: 4px; }
.ai-sg { display: flex; gap: 4px; align-items: stretch; }
.ai-sg-text { flex: 1; text-align: left; border: 1px solid var(--ai-line); border-radius: 8px; background: var(--ai-soft); color: var(--ai-ink); font-size: 12px; line-height: 1.35; padding: 5px 8px; cursor: pointer; }
.ai-sg-text:hover { background: var(--ai-soft-2); }
.ai-sg-try { flex: none; border: 1px solid #5b3fd1; border-radius: 8px; background: #5b3fd1; color: #fff; font-size: 12px; font-weight: 600; padding: 0 9px; cursor: pointer; white-space: nowrap; }
.ai-sg-try:hover { background: #4a30b8; }
#sel-export { max-width: 110px; }
.sel-box { fill: rgba(31, 111, 235, 0.08); stroke: var(--accent); stroke-width: 1.2; stroke-dasharray: 5 4; vector-effect: non-scaling-stroke; }

/* Hosted web app: sign-in gate, account menu, My circuits */
html.auth-wait body { visibility: hidden; }
#btn-account { width: 34px; height: 34px; padding: 0; border-radius: 50%; overflow: hidden; display: grid; place-items: center; font-weight: 700; }
#btn-account img { width: 100%; height: 100%; object-fit: cover; }
.acct-menu { position: fixed; z-index: 10000; min-width: 230px; background: var(--surface); border: 1px solid var(--line-2); border-radius: 10px; box-shadow: 0 14px 40px -12px rgba(0, 0, 0, 0.35); padding: 6px; display: grid; }
.acct-menu .acct-who { padding: 8px 10px 10px; border-bottom: 1px solid var(--line); margin-bottom: 4px; }
.acct-menu .acct-who small { display: block; color: var(--ink-2); }
.acct-menu button, .acct-menu a { text-align: left; border: 0; background: none; padding: 8px 10px; border-radius: 6px; cursor: pointer; color: var(--ink); text-decoration: none; font-size: 13.5px; }
.acct-menu button:hover, .acct-menu a:hover { background: var(--surface-2); }
.panel-cloud { width: 420px; }
.panel-cloud .pb { display: grid; gap: 8px; }
.cl-save { display: flex; gap: 6px; }
.cl-name { flex: 1; height: 30px; border: 1px solid var(--line-2); border-radius: 6px; background: var(--field); color: var(--ink); padding: 0 8px; }
.panel-cloud button { height: 30px; padding: 0 10px; border: 1px solid var(--line-2); border-radius: 6px; background: var(--field); color: var(--ink); cursor: pointer; }
.panel-cloud button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.panel-cloud button.danger { color: var(--danger); }
.cl-msg { margin: 0; font-size: 12.5px; min-height: 16px; color: var(--ink-2); }
.cl-msg.bad { color: var(--danger); }
.cl-list { max-height: 360px; overflow: auto; display: grid; gap: 4px; }
.cl-row { display: flex; gap: 6px; align-items: center; padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; }
.cl-row > div { flex: 1; min-width: 0; }
.cl-row b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cl-row small { color: var(--ink-2); font-size: 11.5px; }
.cl-row.cur { border-color: var(--accent); background: var(--accent-soft); }
.acct-menu[hidden] { display: none; }

/* ---------- Phone layout (body.m, set by js/mobile.js) ---------- */
body.m { overflow: hidden; width: 100vw; }
body.m .topbar { flex-wrap: nowrap; gap: 6px; padding: 6px 8px; }
body.m .brand { margin-right: 0; }
body.m .brand > div { display: none; }
body.m .logo { width: 30px; height: 30px; }
body.m .topbar button, body.m .topbar select { height: 34px; padding: 0 10px; }
body.m #btn-parts { font-weight: 700; color: var(--accent); }
body.m #btn-tut, body.m #btn-ai { width: 36px; padding: 0; font-size: 16px; }
body.m .power { padding: 0 14px 0 10px !important; }
body.m .tb-row2 {
  display: flex; gap: 6px; align-items: center; padding: 6px 8px; overflow-x: auto; overflow-y: hidden; scrollbar-width: none;
  background: var(--surface); border-bottom: 1px solid var(--line); -webkit-overflow-scrolling: touch;
}
body.m .tb-row2::-webkit-scrollbar { display: none; }
body.m .tb-row2 .tb-group { flex: none; border-left: 0; padding-left: 0; gap: 4px; }
body.m .tb-row2 .tb-group + .tb-group { border-left: 1px solid var(--line); padding-left: 6px; }
body.m .tb-row2 button, body.m .tb-row2 select { height: 34px; padding: 0 10px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); white-space: nowrap; }
body.m .tb-row2 button.icon { width: 36px; padding: 0; }
body.m .tb-row2 #sel-examples { max-width: 150px; }
body.m .speed .lbl { display: none; }
body.m .statusbar { font-size: 11.5px; padding: 4px 8px; }
body.m #status-msg { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* palette drawer */
body.m #palette {
  position: fixed; z-index: 9800; top: 0; bottom: 0; left: 0; width: min(260px, 82vw); display: block;
  transform: translateX(-102%); transition: transform .22s ease; box-shadow: 8px 0 30px rgba(0, 0, 0, .25);
}
body.m.pal-open #palette { transform: none; }
body.m .m-shade { position: fixed; inset: 0; z-index: 9790; background: rgba(0, 0, 0, .35); display: none; }
body.m.pal-open .m-shade { display: block; }
body.m .pal-item { min-height: 64px; }

/* properties bottom sheet */
body.m #props {
  display: block; position: fixed; z-index: 9700; left: 0; right: 0; bottom: 0; width: auto; max-height: 46vh;
  border-left: 0; border-top: 1px solid var(--line-2); border-radius: 14px 14px 0 0; box-shadow: 0 -10px 30px rgba(0, 0, 0, .25);
  transform: translateY(105%); transition: transform .22s ease; padding-bottom: env(safe-area-inset-bottom);
}
body.m.sheet-open #props { transform: none; }
body.m .sheet-x { position: sticky; top: 0; float: right; margin: 6px 8px 0 0; width: 34px; height: 34px; border-radius: 50%; border: 1px solid var(--line);
  background: var(--surface-2); font-size: 20px; line-height: 1; cursor: pointer; z-index: 1; }
body.m .pp-row input, body.m .pp-row select { font-size: 16px; } /* 16 px stops phones from zooming into fields */

/* panels and drawers fit the screen */
body.m .panel { max-width: calc(100vw - 12px); }
body.m .panel .pb { overflow-x: auto; }
body.m .scope-screen, body.m .bode-screen, body.m .la-screen { height: 210px; }
body.m .scope-ctl, body.m .lc-main { grid-template-columns: 1fr; }
body.m .tut-coach { left: 6px; right: 6px; width: auto; bottom: 40px; }
body.m #ai-drawer { width: 100%; }
body.m .zoom { bottom: 12px; }
body.m .acct-menu { max-width: calc(100vw - 16px); }
body.m .panel { max-height: calc(100dvh - 104px); display: flex; flex-direction: column; }
body.m .panel .pb { overflow: auto; min-height: 0; }

/* Sign-in gate in the desktop and Android apps */
#app-gate { position: fixed; inset: 0; z-index: 20000; display: grid; place-items: center; padding: 16px; background: var(--bg); overflow: auto; }
.gate-card { width: min(420px, 100%); padding: 26px 22px; text-align: center; background: var(--surface); border: 1px solid var(--line-2); border-radius: 18px; box-shadow: 0 18px 50px -24px rgba(0, 0, 0, .45); }
.gate-logo { width: 72px; height: 72px; }
.gate-card h1 { font-size: 24px; margin: 8px 0 12px; }
.gate-card p { color: var(--ink-2); line-height: 1.5; margin: 0 0 14px; font-size: 14px; }
.gate-card p.gate-strong { color: var(--ink); font-weight: 600; font-size: 15px; }
.gate-card p.gate-err { color: var(--danger); margin: 12px 0 0; }
.gate-google { display: flex; align-items: center; justify-content: center; gap: 12px; width: 100%; height: 50px; border-radius: 12px; border: 1px solid var(--line-2); background: var(--surface); color: var(--ink); font-size: 16px; font-weight: 600; cursor: pointer; }
.gate-google svg { width: 22px; height: 22px; }
.gate-link { background: none; border: 0; color: var(--accent); cursor: pointer; font-size: 14px; padding: 6px; }
.gate-who { display: flex; gap: 10px; align-items: center; justify-content: center; margin-bottom: 14px; text-align: left; }
.gate-who img { width: 42px; height: 42px; border-radius: 50%; }
.gate-who small { display: block; color: var(--ink-2); }
.gate-lang { display: flex; justify-content: center; gap: 6px; margin-top: 16px; }
.gate-lang button { border: 1px solid var(--line-2); background: none; color: var(--ink-2); border-radius: 999px; padding: 3px 12px; font-size: 12.5px; cursor: pointer; }
.gate-lang button.on { background: var(--accent); border-color: var(--accent); color: #fff; }
