:root {
  --bg:#0d1117; --panel:#161b24; --panel2:#1c2330; --border:#262f3d; --text:#e6edf3; --muted:#8b98a8;
  --accent:#4cc9f0; --accent-ink:#04202a; --ok:#4ade80; --bad:#f87171; --warn:#fbbf24;
  color-scheme: dark;
}
* { box-sizing:border-box; }
html, body { height:100%; }
body { margin:0; font-family:Inter,system-ui,-apple-system,"Segoe UI",sans-serif; background:var(--bg); color:var(--text); font-size:14px; }
button, select, textarea { font:inherit; color:inherit; }
button { cursor:pointer; }

.app { height:100dvh; display:flex; flex-direction:column; min-height:0; }

/* ---------- Yläpalkki ---------- */
.topbar { display:flex; align-items:center; gap:14px; padding:8px 14px; background:var(--panel); border-bottom:1px solid var(--border); flex-wrap:wrap; }
.brand { display:flex; align-items:center; gap:8px; font-weight:700; font-size:16px; letter-spacing:.2px; white-space:nowrap; }
.brand .logo { width:26px; height:26px; border-radius:7px; background:linear-gradient(135deg,#4cc9f0,#2bb673); display:grid; place-items:center; font-size:15px; color:#fff; }
.topbar select { background:var(--panel2); border:1px solid var(--border); border-radius:8px; padding:7px 10px; min-width:230px; max-width:380px; }
.runctl { display:flex; align-items:center; gap:8px; margin-left:auto; flex-wrap:wrap; }
.btn { background:var(--panel2); border:1px solid var(--border); border-radius:8px; padding:7px 12px; transition:background .12s,border-color .12s; }
.btn:hover { background:#252e3d; border-color:#34405a; }
.btn.primary { background:var(--accent); color:var(--accent-ink); border-color:var(--accent); font-weight:600; }
.btn.primary:hover { background:#6bd5f5; }
.runctl label { color:var(--muted); display:flex; align-items:center; gap:6px; font-size:13px; }
.runctl select { min-width:0; padding:6px 8px; }
.clock { font-variant-numeric:tabular-nums; color:var(--muted); min-width:74px; text-align:right; }
.toplink { color:var(--accent); font-size:13px; text-decoration:none; white-space:nowrap; }
.toplink:hover { text-decoration:underline; }

/* ---------- Pääalue ---------- */
.main { flex:1; min-height:0; display:grid; grid-template-columns:156px minmax(0,1fr) var(--side-w,370px); }

/* Paletti */
.palette { overflow-y:auto; padding:10px 8px 16px; background:var(--panel); border-right:1px solid var(--border); }
.pgroup { margin-bottom:12px; }
.pgtitle { font-size:10.5px; text-transform:uppercase; letter-spacing:.8px; color:var(--muted); margin:2px 4px 6px; }
.pgrid { display:grid; grid-template-columns:1fr 1fr; gap:5px; }
.tool { display:flex; flex-direction:column; align-items:center; gap:1px; padding:6px 2px 5px; background:var(--panel2); border:1px solid transparent; border-radius:9px; color:#b4c0cf; font-size:10.5px; line-height:1.15; min-width:0; transition:background .1s,color .1s; }
.tool span { max-width:100%; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
.tool:hover:not(:disabled) { background:#27324a; color:#fff; }
.tool.active { background:var(--accent); color:var(--accent-ink); border-color:#9be4fb; font-weight:600; }
.tool:disabled { opacity:.25; cursor:not-allowed; }
.tool { touch-action:pan-y; }                        /* pystyveto vierittää palettia, sivuveto raahaa osan */
body.part-drag, body.part-drag * { cursor:grabbing !important; user-select:none; -webkit-user-select:none; }

/* Piirtoalue */
.canvas { position:relative; display:flex; flex-direction:column; min-width:0; min-height:0; background:#080b10; }
.splitter { position:absolute; top:0; right:-4px; bottom:0; width:8px; z-index:3; cursor:col-resize; touch-action:none; }
.splitter::after { content:""; position:absolute; top:0; bottom:0; left:3px; width:2px; background:transparent; transition:background .12s; }
.splitter:hover::after, .splitter:focus-visible::after, .splitter.dragging::after { background:var(--accent); }
.splitter:focus-visible { outline:none; }
.stage { position:relative; flex:1; min-height:0; overflow:hidden; }
#board { position:absolute; inset:0; width:100%; height:100%; display:block; touch-action:none; user-select:none; -webkit-user-select:none; }
.floating { position:absolute; display:flex; gap:6px; z-index:2; }
.floating.tl { top:10px; left:10px; }
.floating.br { bottom:10px; right:10px; }
.floating .btn { background:rgba(22,27,36,.92); backdrop-filter:blur(4px); padding:6px 10px; }
.floating .btn.sq { width:34px; padding:6px 0; font-size:16px; line-height:1; }
.hint:empty { display:none; }
.hint { position:absolute; top:10px; left:264px; max-width:min(calc(100% - 274px),560px); background:rgba(22,27,36,.92); border:1px solid var(--border); color:#c5d0de; padding:6px 12px; border-radius:14px; font-size:12.5px; pointer-events:none; z-index:1; }
.msg { position:absolute; left:10px; bottom:10px; max-width:calc(100% - 140px); font-size:13px; padding:6px 10px; border-radius:8px; background:rgba(22,27,36,.92); color:var(--muted); z-index:1; display:none; }
.msg.show { display:block; }
.msg.err { color:var(--bad); border:1px solid #5a2630; }
#scopeBox { display:none; background:var(--panel); border-top:1px solid var(--border); padding:6px 10px 8px; }
#scopeBox .scopehead { font-size:11px; text-transform:uppercase; letter-spacing:.8px; color:var(--muted); margin-bottom:2px; }
#scopeBox svg { width:100%; height:auto; max-height:170px; display:block; }

.savestate { color:var(--muted); font-size:12.5px; opacity:0; transition:opacity .25s; white-space:nowrap; }
.savestate.show { opacity:1; }
#howCircuit { color:var(--text); }
.hownote { display:block; width:100%; text-align:left; padding:8px 10px; margin-top:6px; background:var(--panel2); border:1px solid transparent; border-radius:8px; }
.hownote:hover, .hownote:focus-visible { border-color:var(--accent); outline:none; }
.hownote code { display:block; font:12px/1.45 ui-monospace,"Cascadia Code",Consolas,monospace; color:#9fe6b0; white-space:pre-wrap; overflow-wrap:anywhere; }
.hownote span { display:block; margin-top:3px; font-size:13px; line-height:1.45; color:#c5d0de; }
.menu { position:relative; }
.menu summary { list-style:none; cursor:pointer; user-select:none; }
.menu summary::-webkit-details-marker { display:none; }
.menu summary::after { content:"▾"; margin-left:6px; color:var(--muted); }
.menubox { position:absolute; top:calc(100% + 6px); left:0; z-index:10; min-width:130px; padding:5px; display:flex; flex-direction:column; background:var(--panel2); border:1px solid var(--border); border-radius:10px; box-shadow:0 12px 32px rgba(0,0,0,.45); }
.menubox button { text-align:left; padding:7px 10px; background:none; border:0; border-radius:6px; }
.menubox button:hover { background:#2a3548; }
a.brand { color:inherit; text-decoration:none; }
.btn.accent { border-color:#3c6d86; color:#bdeaf9; }

/* Esimerkkikirjasto */
.modal { position:fixed; inset:0; z-index:20; display:none; align-items:center; justify-content:center; padding:24px; background:rgba(4,7,12,.72); backdrop-filter:blur(4px); }
.modal.show { display:flex; }
.modalbox { width:min(1120px,100%); max-height:100%; display:flex; flex-direction:column; background:var(--panel); border:1px solid var(--border); border-radius:16px; overflow:hidden; box-shadow:0 24px 80px rgba(0,0,0,.55); }
.modalhead { display:flex; align-items:center; gap:14px; padding:14px 18px; border-bottom:1px solid var(--border); }
.modalhead h2 { margin:0; font-size:17px; white-space:nowrap; }
.modalhead input { flex:1; min-width:0; background:var(--panel2); color:var(--text); border:1px solid var(--border); border-radius:8px; padding:8px 12px; font:inherit; }
.modalhead input:focus { outline:none; border-color:var(--accent); }
.modalbody { overflow-y:auto; padding:6px 18px 20px; }
.modalbody h3 { font-size:12px; text-transform:uppercase; letter-spacing:.8px; color:var(--muted); margin:18px 0 10px; font-weight:600; }
.modalbody h3 small { font-size:11px; opacity:.7; margin-left:4px; }
.libgrid { display:grid; grid-template-columns:repeat(auto-fill,minmax(250px,1fr)); gap:10px; }
.libcard { display:flex; flex-direction:column; gap:6px; text-align:left; padding:12px 14px; background:var(--panel2); border:1px solid transparent; border-radius:11px; transition:border-color .12s,background .12s; }
.libcard:hover, .libcard:focus-visible { border-color:var(--accent); background:#222b3a; outline:none; }
.libcard b { font-size:14px; font-weight:600; }
.libcard span { color:var(--muted); font-size:12.5px; line-height:1.4; display:-webkit-box; -webkit-box-orient:vertical; -webkit-line-clamp:3; overflow:hidden; }
.libthumb { display:block; width:100%; height:118px; margin-bottom:4px; padding:6px; background:#0d1219; border-radius:8px; }
.runctl label.check { cursor:pointer; }
.floating.opts { top:52px; left:10px; align-items:center; background:rgba(22,27,36,.92); border:1px solid var(--border); border-radius:8px; padding:5px 8px; font-size:12.5px; color:var(--muted); }
.floating.opts:empty { display:none; }
.floating.opts input[type=range] { width:150px; accent-color:var(--accent); }
#sensorVal { min-width:48px; color:var(--text); font-variant-numeric:tabular-nums; }
details a { color:var(--accent); }
.optbtn { background:var(--panel2); border:1px solid var(--border); border-radius:6px; padding:3px 8px; font-size:12px; }
.optbtn.on { background:var(--accent); color:var(--accent-ink); border-color:var(--accent); font-weight:600; }
.swatch { width:20px; height:20px; border-radius:50%; border:2px solid transparent; padding:0; }
.swatch.on { border-color:#fff; box-shadow:0 0 0 1px #000; }

/* Sivupaneeli */
.side { overflow-y:auto; padding:12px; background:var(--bg); border-left:1px solid var(--border); }
.card { background:var(--panel); border:1px solid var(--border); border-radius:12px; padding:14px; margin-bottom:12px; }
.card h2 { font-size:14px; margin:0 0 6px; }
.card h3 { font-size:12px; text-transform:uppercase; letter-spacing:.8px; color:var(--muted); margin:14px 0 6px; font-weight:600; }
.card p { margin:6px 0; line-height:1.5; font-size:13.5px; }
.muted { color:var(--muted); }
ul.checks { list-style:none; padding:0; margin:10px 0 0; }
ul.checks li { padding:5px 0 5px 24px; position:relative; font-size:13.5px; }
ul.checks li::before { position:absolute; left:2px; top:5px; width:16px; height:16px; border-radius:50%; text-align:center; font-size:11px; line-height:16px; }
ul.checks li.ok { color:var(--ok); }
ul.checks li.ok::before { content:"✔"; background:#14361f; }
ul.checks li.bad { color:var(--muted); }
ul.checks li.bad::before { content:""; border:1.5px solid #3a4658; }
.done { margin-top:10px; padding:10px 12px; border-radius:10px; background:#12351f; color:var(--ok); font-weight:600; display:none; }
.done.show { display:block; }
textarea#code { width:100%; height:240px; background:#0a0e14; color:#d6e2f0; border:1px solid var(--border); border-radius:8px; padding:10px; font:13px/1.5 ui-monospace,"Cascadia Code",Consolas,monospace; resize:vertical; tab-size:2; white-space:pre; overflow:auto; }
textarea#code:focus { outline:none; border-color:var(--accent); }
.codeErr { color:var(--bad); font-size:12.5px; min-height:1.2em; margin-top:6px; font-family:ui-monospace,Consolas,monospace; }
.stale { display:none; color:var(--warn); font-size:12.5px; margin-top:4px; }
pre#serial { margin:0; height:110px; overflow:auto; background:#0a0e14; border:1px solid var(--border); border-radius:8px; padding:8px 10px; font:12px/1.45 ui-monospace,Consolas,monospace; color:#9fe6b0; white-space:pre-wrap; }
details summary { cursor:pointer; font-weight:600; font-size:14px; }
details p { color:var(--muted); }

/* ---------- Kapeat näytöt ---------- */
@media (max-width: 1100px) { .main { grid-template-columns:140px minmax(0,1fr) var(--side-w,320px); } }
@media (max-width: 900px) {
  html, body { height:auto; }
  .app { height:auto; min-height:100dvh; }
  .main { display:flex; flex-direction:column; }
  .palette { display:flex; gap:10px; overflow-x:auto; overflow-y:hidden; border-right:0; border-bottom:1px solid var(--border); padding:8px; }
  .pgroup { margin:0; flex:none; }
  .pgrid { display:flex; }
  .tool { width:62px; touch-action:pan-x; }
  .canvas { height:62vh; }
  .side { border-left:0; overflow:visible; }
  .splitter { display:none; }
  .topbar select { min-width:0; flex:1; }
  .runctl { margin-left:0; }
  .hint { display:none; }
}
