:root {
  --bg: #eef1f5;
  --panel: #ffffff;
  --border: #d5dbe3;
  --text: #17202a;
  --muted: #5f6b7a;
  --accent: #2563eb;
  --accent-2: #1d4ed8;
  --ink: #1f2933;
  --paper: #fafbfc;
  --grid: #c3cad4;
  --wire: #1f6feb;
  --sel: #f59e0b;
  --warn: #b45309;
  --danger: #dc2626;
  --ok: #15803d;
  --top: #0f172a;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  color: var(--text);
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; background: var(--bg); overflow: hidden; }
button { font: inherit; cursor: pointer; border: 1px solid var(--border); background: #fff; color: var(--text);
  border-radius: 8px; padding: 6px 11px; line-height: 1.2; transition: background .12s, border-color .12s; }
button:hover { border-color: var(--accent); background: #f5f8ff; }
button:disabled { opacity: .5; cursor: not-allowed; }
button.primary { background: var(--accent); color: #fff; border-color: var(--accent); font-weight: 600; }
button.primary:hover { background: var(--accent-2); }
button.danger { color: var(--danger); }
input, select, textarea { font: inherit; border: 1px solid var(--border); border-radius: 8px; padding: 6px 9px;
  background: #fff; color: var(--text); width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid #bfdbfe; border-color: var(--accent); }
input[type=range] { padding: 0; accent-color: var(--accent); }

/* ---------- top bar ---------- */
.topbar { height: 50px; background: var(--top); color: #fff; display: flex; align-items: center; gap: 12px; padding: 0 14px; }
.brand { display: flex; align-items: center; gap: 7px; font-weight: 800; white-space: nowrap; color: #93c5fd; font-size: 16px; }
.title-input { max-width: 300px; background: #1e293b; color: #fff; border-color: #334155; }
.top-actions { display: flex; gap: 6px; }
.top-actions > button, .top-actions .dropdown > button { background: #1e293b; color: #e2e8f0; border-color: #334155; }
.top-actions > button:hover, .top-actions .dropdown > button:hover { background: #334155; }
.badge { margin-left: auto; font-size: 12px; padding: 3px 10px; border-radius: 999px; background: #334155; color: #cbd5e1; white-space: nowrap; }
.badge.on { background: #14532d; color: #bbf7d0; }

.dropdown { position: relative; }
.dropdown .menu { display: none; position: absolute; left: 0; top: 100%; background: #fff; border: 1px solid var(--border);
  border-radius: 10px; padding: 5px; z-index: 60; min-width: 230px; box-shadow: 0 10px 28px rgba(15,23,42,.18); }
.dropdown .menu.wide { min-width: 420px; right: 0; left: auto; }
.dropdown.up .menu { top: auto; bottom: 100%; }
.dropdown:hover .menu, .dropdown:focus-within .menu { display: block; }
.dropdown .menu button { display: block; width: 100%; text-align: left; border: 0; border-radius: 6px; color: var(--text); }
.dropdown .menu button:hover { background: #eff6ff; }

/* ---------- toolbar ---------- */
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; padding: 7px 12px; background: var(--panel);
  border-bottom: 1px solid var(--border); }
.tgroup { display: flex; align-items: center; gap: 5px; padding-right: 10px; border-right: 1px solid var(--border); }
.tgroup.right { margin-left: auto; border-right: 0; padding-right: 0; }
.toolbar button { display: inline-flex; align-items: center; gap: 5px; padding: 6px 10px; }
.toolbar .ic { font-size: 16px; }
.toolbar button.on { background: #e0ecff; border-color: #93b4f5; }
.tlabel { font-size: 13px; color: var(--muted); font-weight: 600; }
.swatches { display: flex; gap: 4px; }
.sw { width: 24px; height: 24px; border-radius: 50%; border: 2px solid #fff; box-shadow: 0 0 0 1px #9aa5b4; padding: 0; cursor: pointer; }
.sw.active { box-shadow: 0 0 0 3px var(--accent); }
.sw.auto { background: conic-gradient(#e53935 0 25%, #263238 0 50%, #fdd835 0 75%, #43a047 0); }
#wireStyle { width: auto; padding: 5px 6px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; }
.seg button.on { background: var(--accent); color: #fff; }
#simBtn.start { background: #16a34a; color: #fff; border-color: #16a34a; font-weight: 700; padding: 7px 14px; }
#simBtn.start:hover { background: #15803d; }
#simBtn.stop { background: #dc2626; color: #fff; border-color: #dc2626; font-weight: 700; padding: 7px 14px; }
.simtime { font-family: ui-monospace, monospace; font-size: 15px; background: #0f172a; color: #4ade80; padding: 5px 10px; border-radius: 8px; min-width: 86px; text-align: center; }

/* ---------- prompt bar ---------- */
.promptbar { display: flex; gap: 8px; align-items: center; padding: 8px 12px; background: linear-gradient(90deg, #eef2ff, #f0f9ff);
  border-bottom: 1px solid var(--border); }
.promptbar .spark { font-size: 20px; }
.promptbar input { font-size: 15px; padding: 8px 12px; border-radius: 10px; }
.promptbar .menu button { white-space: normal; }
.promptbar > button, .promptbar .dropdown > button, .nowrap { white-space: nowrap; flex: 0 0 auto; }
.keypad-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-bottom: 12px; }
.keypad-grid button { font-size: 17px; font-weight: 700; padding: 10px 0; }
.ctrl-toggle { width: 100%; margin-bottom: 8px; }

/* ---------- layout ---------- */
body { display: flex; flex-direction: column; }
.topbar, .toolbar, .promptbar { flex: 0 0 auto; }
.layout { display: grid; grid-template-columns: 270px 1fr 350px; flex: 1; min-height: 0; }
.sidebar, .panel { background: var(--panel); overflow: auto; }
.sidebar { border-right: 1px solid var(--border); }
.panel { border-left: 1px solid var(--border); display: flex; flex-direction: column; }

/* ---------- sidebar ---------- */
.side-head { position: sticky; top: 0; background: var(--panel); padding: 10px; border-bottom: 1px solid var(--border); z-index: 2; }
.hint { color: var(--muted); font-size: 12px; margin-top: 6px; }
.cat { border-bottom: 1px solid var(--border); }
.cat > summary { padding: 9px 12px; font-weight: 700; cursor: pointer; list-style: none; display: flex; align-items: center; gap: 8px; font-size: 14px; }
.cat > summary .chip { width: 10px; height: 10px; border-radius: 3px; }
.cat > summary .n { margin-left: auto; color: var(--muted); font-weight: 400; font-size: 12px; }
.cat > summary::after { content: "▸"; color: var(--muted); }
.cat[open] > summary::after { content: "▾"; }
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; padding: 0 10px 12px; }
.tile { border: 1px solid var(--border); border-radius: 10px; padding: 7px 5px 6px; text-align: center; cursor: grab;
  background: #fff; user-select: none; font-size: 12.5px; line-height: 1.25; font-weight: 500; transition: transform .1s, box-shadow .1s; }
.tile:hover { border-color: var(--accent); box-shadow: 0 4px 12px rgba(37,99,235,.15); transform: translateY(-1px); }
.tile .icon { width: 100%; height: 58px; display: block; margin: 0 auto 4px; background: #f6f8fb; border-radius: 6px; }

/* ---------- center ---------- */
.center { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.canvas-wrap { position: relative; flex: 1; overflow: hidden; background: #f7f8fa; min-height: 120px; }
#canvas { width: 100%; height: 100%; display: block; cursor: default; touch-action: none; user-select: none; }
#canvas.panning { cursor: grabbing; }
#canvas.wiring { cursor: crosshair; }
.empty-hint { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; pointer-events: none; }
.eh-box { background: rgba(255,255,255,.85); border: 1px dashed #b6c2d2; border-radius: 14px; padding: 18px 26px; line-height: 2; color: var(--muted); }
.eh-title { font-weight: 800; color: var(--text); font-size: 17px; }
.busy { position: absolute; inset: 0; background: rgba(255,255,255,.75); display: flex; align-items: center; justify-content: center; gap: 10px; font-weight: 600; z-index: 5; }
.spinner { width: 24px; height: 24px; border: 3px solid #bfdbfe; border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.sim-banner { position: absolute; top: 10px; left: 50%; transform: translateX(-50%); background: #111827e6; color: #fff; padding: 6px 14px;
  border-radius: 999px; font-size: 13px; z-index: 4; pointer-events: none; }
.statusbar { height: 26px; display: flex; gap: 14px; align-items: center; padding: 0 10px; font-size: 12.5px; color: var(--muted);
  background: var(--panel); border-top: 1px solid var(--border); }
.grow { flex: 1; }

/* ---------- drawer (code / serial) ---------- */
.drawer { height: 290px; display: flex; flex-direction: column; border-top: 2px solid #334155; background: #0f172a; color: #e2e8f0; }
.drawer-head { display: flex; align-items: center; gap: 8px; padding: 5px 8px; background: #1e293b; }
.drawer-head button { background: #334155; color: #e2e8f0; border-color: #475569; padding: 4px 10px; font-size: 13px; }
.drawer-head button:hover { background: #475569; }
.dtabs { display: flex; gap: 4px; }
.dtabs button.on { background: var(--accent); border-color: var(--accent); }
.dactions { display: flex; gap: 6px; margin-left: 8px; }
.drawer-head .close { margin-left: auto; }
.drawer-head .menu { background: #fff; }
.drawer-body { flex: 1; min-height: 0; display: flex; }
.code-pane { flex: 1; display: flex; min-width: 0; }
.gutter { margin: 0; padding: 10px 8px; background: #111827; color: #64748b; text-align: right; font: 13.5px/1.5 ui-monospace, Consolas, monospace;
  overflow: hidden; user-select: none; min-width: 44px; }
#code { flex: 1; border: 0; border-radius: 0; background: #0f172a; color: #e2e8f0; font: 13.5px/1.5 ui-monospace, Consolas, monospace;
  padding: 10px; resize: none; tab-size: 2; white-space: pre; overflow: auto; }
#code:focus { outline: none; }
.serial-pane { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#serialOut { flex: 1; margin: 0; padding: 10px; overflow: auto; font: 13px/1.45 ui-monospace, Consolas, monospace; color: #a7f3d0; white-space: pre-wrap; }
.serial-in { display: flex; gap: 6px; padding: 6px; background: #1e293b; }
.serial-in input { background: #0f172a; color: #e2e8f0; border-color: #334155; }
.serial-in button { background: var(--accent); color: #fff; border: 0; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: #4ade80; }

/* ---------- schematic / realistic ---------- */
.sym line, .sym rect, .sym circle, .sym path { stroke: var(--ink); stroke-width: 2; fill: var(--paper); stroke-linecap: round; }
.sym line { fill: none; }
.sym .solid { fill: var(--ink); stroke: var(--ink); }
.sym .fillable { fill: var(--paper); }
.sym .glow { stroke: none !important; opacity: 0; filter: blur(4px); pointer-events: none; }
.sym text { fill: var(--ink); stroke: none; font-family: system-ui, sans-serif; }
.sym text.pol { font-size: 13px; font-weight: 700; }
.sym text.mid { font-size: 16px; font-weight: 700; text-anchor: middle; }
.sym text.small { font-size: 11px; font-weight: 700; text-anchor: middle; }
.sym text.pin-l { font-size: 9px; text-anchor: start; }
.sym text.pin-r { font-size: 9px; text-anchor: end; }
.sym text.pinno { font-size: 8px; text-anchor: middle; fill: var(--muted); }
.sym.colorful line, .sym.colorful rect, .sym.colorful circle, .sym.colorful path { stroke: #334155; }
.comp.lit .glow { opacity: .55; }
.comp.lit.led .fillable { fill: var(--led-color, #ef4444); }

.comp { cursor: move; }
.comp .hit { fill: transparent; stroke: none; }
.comp.selected .hit { fill: rgba(245, 158, 11, .08); stroke: var(--sel); stroke-width: 2; stroke-dasharray: 6 4; }
.comp.has-error .hit { stroke: var(--danger); stroke-width: 2.5; stroke-dasharray: none; }
.lbl { font-size: 12px; font-weight: 700; fill: #1e293b; pointer-events: none; paint-order: stroke; stroke: #fff; stroke-width: 3px; }
.val { font-size: 11px; fill: #475569; pointer-events: none; paint-order: stroke; stroke: #fff; stroke-width: 3px; }
.meas { font-size: 11px; fill: #0f766e; font-weight: 700; pointer-events: none; paint-order: stroke; stroke: #fff; stroke-width: 3px; }
.meas.warn { fill: var(--danger); }
.err-badge circle { fill: var(--danger); stroke: #fff; stroke-width: 2; }
.err-badge text { fill: #fff; font-weight: 800; font-size: 13px; text-anchor: middle; }
.warn-badge circle { fill: #f59e0b; }

.pin { fill: #fff; stroke: var(--wire); stroke-width: 1.5; cursor: crosshair; opacity: 0; }
.comp:hover .pin, .wiring .pin { opacity: 1; }
.pin.free { opacity: .7; fill: #fef3c7; stroke: #d97706; }
.pin:hover { fill: #22c55e; stroke: #15803d; opacity: 1; }
.pin.hole { fill: #3a3a3a; stroke: none; opacity: 1; }
.pin.hole:hover { fill: #22c55e; }
.bb-hl { fill: none; stroke: #22c55e; stroke-width: 3; pointer-events: none; }
.bb-hl-bg { fill: rgba(34,197,94,.18); stroke: #22c55e; stroke-width: 1.5; pointer-events: none; }
.snap-ok { fill: #22c55e; opacity: .85; pointer-events: none; }
.snap-bad { fill: none; stroke: #ef4444; stroke-width: 2; pointer-events: none; }

.wire { fill: none; stroke-width: 4; stroke-linejoin: round; stroke-linecap: round; }
.wire-outline { fill: none; stroke: rgba(0,0,0,.35); stroke-width: 6; stroke-linejoin: round; stroke-linecap: round; }
.schem .wire { stroke-width: 2.4; }
.schem .wire-outline { display: none; }
.wire-hit { fill: none; stroke: transparent; stroke-width: 12; cursor: pointer; }
.wire-g.selected .wire-outline { stroke: var(--sel); stroke-width: 9; display: inline; }
.wire.flow { stroke-dasharray: 8 6; animation: flow .7s linear infinite; }
@keyframes flow { to { stroke-dashoffset: -14; } }
.wire-end { stroke: rgba(0,0,0,.4); stroke-width: 1; }
.junction { fill: #334155; }
.preview { fill: none; stroke: var(--accent); stroke-width: 3; stroke-dasharray: 6 4; pointer-events: none; }
.vtag { font-size: 10.5px; fill: #6d28d9; font-weight: 700; pointer-events: none; }
.vtag-bg { fill: #f5f3ff; stroke: #c4b5fd; }
.spin { animation: spin 0.5s linear infinite; }
.pulse { animation: pulse .5s ease-in-out infinite alternate; }
@keyframes pulse { from { opacity: .2; } to { opacity: 1; } }

/* ---------- right panel ---------- */
.tabs { display: flex; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--panel); z-index: 2; }
.tabs button { flex: 1; border: 0; border-radius: 0; padding: 11px 2px; border-bottom: 3px solid transparent; background: none; font-size: 13px; }
.tabs button.active { border-bottom-color: var(--accent); color: var(--accent); font-weight: 700; }
.count { display: inline-block; min-width: 18px; padding: 0 5px; border-radius: 999px; background: var(--danger); color: #fff; font-size: 11px; font-weight: 700; }
.count.warn { background: #f59e0b; }
.count.ok { background: var(--ok); }
.tab { display: none; padding: 14px; overflow: auto; }
.tab.active { display: block; }
.field { margin-bottom: 12px; }
.field label { display: block; font-size: 12.5px; color: var(--muted); margin-bottom: 4px; font-weight: 600; }
.row { display: flex; gap: 6px; align-items: center; }
.row > * { flex: 1; }
.comp-head { display: flex; gap: 12px; align-items: center; margin-bottom: 14px; }
.comp-head .icon { width: 84px; height: 64px; border: 1px solid var(--border); border-radius: 10px; background: #f6f8fb; flex: 0 0 auto; }
.comp-head h3 { margin: 0; font-size: 17px; }
.desc { color: var(--muted); font-size: 13px; line-height: 1.55; }
.live { background: #ecfdf5; border: 1px solid #a7f3d0; border-radius: 10px; padding: 10px; margin-bottom: 12px; }
.live b { color: #065f46; }
.bigbtn { width: 100%; padding: 12px; font-size: 15px; font-weight: 700; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 6px 4px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-weight: 600; font-size: 12px; }
td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.issue { border: 1px solid var(--border); border-left: 5px solid var(--muted); border-radius: 10px; padding: 10px 12px; margin-bottom: 10px; background: #fff; }
.issue.error { border-left-color: var(--danger); background: #fef2f2; }
.issue.warn { border-left-color: #f59e0b; background: #fffbeb; }
.issue.info { border-left-color: #3b82f6; background: #eff6ff; }
.issue .it { font-weight: 700; display: flex; gap: 6px; align-items: flex-start; }
.issue .why { margin: 6px 0; font-size: 13.5px; line-height: 1.55; }
.issue .fix { font-size: 13.5px; }
.issue .fix b { color: #065f46; }
.issue ul { margin: 4px 0 0; padding-left: 20px; line-height: 1.55; }
.issue .goto { margin-top: 6px; font-size: 12.5px; padding: 3px 9px; }
.okbox { background: #f0fdf4; border: 1px solid #bbf7d0; color: var(--ok); padding: 10px 12px; border-radius: 10px; margin-bottom: 10px; font-weight: 600; }
.ai-box { white-space: pre-wrap; line-height: 1.6; font-size: 13.5px; background: #f8fafc; border: 1px solid #c7d2fe; border-radius: 10px; padding: 12px; }
.explain { white-space: pre-wrap; line-height: 1.65; font-size: 14px; background: #f8fafc; border: 1px solid var(--border); border-radius: 10px; padding: 12px; }
h4 { margin: 16px 0 8px; font-size: 14px; }
pre.netlist { background: #0f172a; color: #e2e8f0; padding: 10px; border-radius: 8px; font-size: 12px; overflow: auto; }
.stat { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin: 8px 0; }
.stat div { background: #f8fafc; border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; }
.stat b { display: block; font-size: 20px; }
.kbd { font-family: ui-monospace, monospace; background: #f1f5f9; border: 1px solid var(--border); border-radius: 4px; padding: 0 5px; font-size: 12px; }
.shortcuts td { padding: 4px; }

/* ---------- modal / toast ---------- */
.modal { position: fixed; inset: 0; background: rgba(15,23,42,.45); display: flex; align-items: center; justify-content: center; z-index: 100; }
.modal-box { background: #fff; border-radius: 14px; width: min(540px, 92vw); max-height: 80vh; overflow: auto; padding: 16px 18px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.modal-head button { border: 0; }
.list-item { display: flex; gap: 8px; align-items: center; padding: 7px 0; border-bottom: 1px solid var(--border); }
.list-item .name { flex: 1; cursor: pointer; color: var(--accent); font-weight: 600; }
.toast { position: fixed; bottom: 44px; left: 50%; transform: translateX(-50%); background: #111827; color: #fff;
  padding: 9px 16px; border-radius: 10px; z-index: 200; max-width: 80vw; }

@media (max-width: 1280px) {
  .layout { grid-template-columns: 230px 1fr 310px; }
  .toolbar .lb { display: none; }
  #simBtn .lb { display: inline; }
}
@media (max-width: 860px) {
  html, body { overflow: auto; }
  .layout { grid-template-columns: 1fr; grid-template-rows: 200px 70vh auto; height: auto; }
  .brand span, .title-input { display: none; }
}
