/* Yantra — one stylesheet, dark and light themes through variables. No inline styles anywhere (CSP style-src 'self'). */
:root {
  --bg: #0f141c; --bg2: #151c26; --bg3: #1c2532; --line: #2a3546; --fg: #e3e9f2; --fg2: #9fb0c4; --accent: #3fd2a0; --accent2: #ffb347; --danger: #ff6b6b; --warn: #ffc857;
  --canvas: #0b1017; --grid: #2a3646; --wire: #5fe08a; --part: #d8e2ef; --part-bg: #121a24; --sel: #4db8ff; --label: #ffb347; --ref: #b9c7d9; --val: #7f93ab;
  --plot-bg: #0c1118; --plot-grid: #233042; --plot-fg: #b8c4d3; --dock-h: 300px; --radius: 8px;
  font-family: Inter, "Noto Sans Devanagari", system-ui, -apple-system, "Segoe UI", sans-serif; font-size: 14px; color-scheme: dark;
}
:root[data-theme="light"] {
  --bg: #f4f6fa; --bg2: #ffffff; --bg3: #eef1f6; --line: #d6dce6; --fg: #1d2433; --fg2: #5b6b80; --accent: #0f9d6e; --accent2: #c76a12; --danger: #d33; --warn: #b7791f;
  --canvas: #fbfcfe; --grid: #c9d3e0; --wire: #1b7f45; --part: #1d2433; --part-bg: #ffffff; --sel: #1673d6; --label: #b8560c; --ref: #1d2433; --val: #5b6b80;
  --plot-bg: #ffffff; --plot-grid: #e2e7ef; --plot-fg: #3d4b5e; color-scheme: light;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg); }
body { overflow: hidden; }
button, input, select, textarea { font: inherit; color: inherit; }
.boot { display: grid; place-items: center; height: 100vh; color: var(--fg2); font-size: 18px; }
.hidden-input { position: fixed; left: -9999px; width: 1px; height: 1px; opacity: 0; }

/* ---------- layout ---------- */
.app { display: grid; height: 100vh; height: 100dvh; width: 100%; overflow: hidden; grid-template-columns: 250px minmax(0, 1fr) 290px; grid-template-rows: auto 1fr 6px var(--dock-h) auto auto;
  grid-template-areas: "top top top" "lib center props" "lib grip props" "lib dock props" "status status status" "pbar pbar pbar"; }
.topbar { grid-area: top; display: flex; align-items: center; gap: 6px; padding: 6px 10px; background: var(--bg2); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.lib { grid-area: lib; background: var(--bg2); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.center { grid-area: center; display: flex; flex-direction: column; min-width: 0; min-height: 0; position: relative; }
.props { grid-area: props; background: var(--bg2); border-left: 1px solid var(--line); overflow: auto; min-height: 0; }
.dock-grip { grid-area: grip; cursor: row-resize; background: var(--line); }
.dock { grid-area: dock; display: flex; flex-direction: column; min-height: 0; background: var(--bg2); }
.status { grid-area: status; display: flex; gap: 14px; padding: 3px 10px; font-size: 12px; color: var(--fg2); background: var(--bg2); border-top: 1px solid var(--line); }
.phone-bar { grid-area: pbar; display: none; }
.spacer { flex: 1; }
.app > * { min-width: 0; }
.brand { display: flex; align-items: center; gap: 6px; margin-right: 8px; }
.b-name { font-weight: 700; font-size: 17px; letter-spacing: .3px; }
.b-hi { color: var(--accent); font-size: 15px; }

/* ---------- buttons, menus ---------- */
.btn { background: var(--bg3); border: 1px solid var(--line); border-radius: 6px; padding: 5px 11px; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: var(--accent); }
.btn:focus-visible, .tool:focus-visible, .chip:focus-visible, .menu-btn:focus-visible { outline: 2px solid var(--sel); outline-offset: 1px; }
.btn.primary { background: var(--accent); color: #06231a; border-color: var(--accent); font-weight: 600; }
:root[data-theme="light"] .btn.primary { color: #fff; }
.btn.live { border-color: var(--accent2); }
.btn.live.on { background: var(--accent2); color: #2a1600; }
.btn.danger { border-color: var(--danger); color: var(--danger); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { border-color: var(--line); }
.btn.small { padding: 2px 8px; font-size: 12px; }
.btn.wide { width: 100%; margin: 8px 0; text-align: center; display: block; text-decoration: none; }
.icon-btn { background: transparent; border: 0; cursor: pointer; color: var(--fg2); font-size: 16px; padding: 2px 6px; }
.menu { position: relative; }
.menu-btn { background: transparent; border: 1px solid transparent; border-radius: 6px; padding: 5px 9px; cursor: pointer; }
.menu-btn:hover { background: var(--bg3); }
.menu-list { position: absolute; top: 100%; left: 0; z-index: 50; background: var(--bg2); border: 1px solid var(--line); border-radius: var(--radius); padding: 4px; min-width: 270px; box-shadow: 0 8px 24px rgba(0,0,0,.35); }
.menu-item { display: block; width: 100%; text-align: left; background: transparent; border: 0; padding: 7px 10px; border-radius: 5px; cursor: pointer; }
.menu-item:hover { background: var(--bg3); }
.menu-list hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; }
.tb-run { display: flex; gap: 6px; margin-left: 8px; }
.chip { background: var(--bg3); border: 1px solid var(--line); border-radius: 14px; padding: 2px 9px; font-size: 12px; cursor: pointer; }
.chip.on { background: var(--accent); color: #06231a; border-color: var(--accent); }

/* ---------- library ---------- */
.panel-head { display: flex; align-items: baseline; justify-content: space-between; padding: 8px 10px 4px; }
.panel-head h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .6px; color: var(--fg2); margin: 0; }
.lib-count { font-size: 11px; color: var(--fg2); }
.lib-search { margin: 0 10px 6px; padding: 6px 9px; border-radius: 6px; border: 1px solid var(--line); background: var(--bg); }
.lib-cats { display: flex; flex-wrap: wrap; gap: 4px; padding: 0 10px 6px; max-height: 92px; overflow: auto; }
.lib-items { overflow: auto; flex: 1; padding: 0 6px 10px; }
.lib-item { display: flex; gap: 8px; align-items: center; padding: 5px; border-radius: 6px; cursor: grab; }
.lib-item:hover, .lib-item:focus { background: var(--bg3); outline: none; }
.part-preview { width: 46px; height: 34px; flex: none; color: var(--part); stroke: currentColor; stroke-width: 1.6; fill: none; stroke-linecap: round; stroke-linejoin: round; }
.lib-name { font-size: 13px; }
.lib-desc { font-size: 11px; color: var(--fg2); }
.lib-tag { font-size: 10px; color: var(--accent2); }

/* ---------- canvas ---------- */
.toolstrip { display: flex; gap: 2px; align-items: center; padding: 4px 6px; background: var(--bg2); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.tool { background: transparent; border: 1px solid transparent; border-radius: 6px; padding: 3px 8px; cursor: pointer; font-size: 13px; }
.tool:hover { background: var(--bg3); }
.tool.on { background: var(--bg3); border-color: var(--accent); }
.sep { width: 1px; height: 20px; background: var(--line); margin: 0 4px; }
.sheet-tabs { display: flex; gap: 4px; }
.canvas-host { flex: 1; position: relative; min-height: 0; background: var(--canvas); }
svg.canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; outline: none; touch-action: none; user-select: none; cursor: default; }
svg.canvas.wiring, svg.canvas.on-pin, svg.canvas.placing { cursor: crosshair; }
svg.canvas.panning { cursor: grab; }
.gdot { fill: var(--grid); }
.gdot.major { fill: var(--fg2); opacity: .45; }
.wire { fill: none; stroke: var(--wire); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.wire.sel { stroke: var(--sel); stroke-width: 3; }
.wire.preview { stroke-dasharray: 5 4; opacity: .85; }
.wire-end { fill: var(--sel); }
.junction { fill: var(--wire); }
.part { color: var(--part); }
.part .sym { stroke: currentColor; stroke-width: 1.6; fill: none; stroke-linecap: round; stroke-linejoin: round; }
.part.sel { color: var(--sel); }
.part.err { color: var(--danger); }
.part.ghost { opacity: .55; color: var(--sel); }
.ptext { fill: var(--part); stroke: none; }
.ptext.b { font-weight: 700; }
.ptext.i { font-style: italic; }
.ptext.ov { text-decoration: overline; }
.ref { fill: var(--ref); font-weight: 600; }
.val { fill: var(--val); }
.lbl { fill: var(--label); font-weight: 600; }
.note text { fill: var(--fg2); font-style: italic; }
.note.sel text { fill: var(--sel); }
.selbox { fill: rgba(77,184,255,.1); stroke: var(--sel); stroke-dasharray: 4 3; }
.pin-hover { fill: none; stroke: var(--sel); stroke-width: 2; }
.glow { stroke: none; filter: blur(2px); }
.seg-on { stroke-width: 4; stroke-linecap: round; }
.lcd-screen { fill: #9bd36b; stroke: #3d5a2a; }
.lcd-text { font-family: ui-monospace, "Courier New", monospace; fill: #142b0e; }
.servo-arm { stroke: var(--accent2); stroke-width: 4; stroke-linecap: round; }
.live-val { fill: var(--accent); font-size: 11px; font-family: ui-monospace, monospace; }
.warn-txt { fill: var(--danger); font-size: 10px; }
.lprobe { stroke: var(--line); }
.lprobe.hi { fill: #ff4d4d; } .lprobe.lo { fill: #2a2f38; } .lprobe.x { fill: #8a6d00; }
.lesson { position: absolute; right: 10px; bottom: 10px; max-width: 400px; max-height: 45%; overflow: auto; background: var(--bg2); border: 1px solid var(--line); border-radius: var(--radius); padding: 8px 12px; font-size: 13px; box-shadow: 0 6px 20px rgba(0,0,0,.3); }
.lesson h3 { margin: 2px 24px 6px 0; font-size: 14px; cursor: pointer; }
.lesson.folded > :not(h3):not(.icon-btn) { display: none; }
.lesson.folded { padding-bottom: 2px; }
.lesson .icon-btn { position: absolute; right: 4px; top: 4px; }
.banner { background: var(--accent2); color: #2a1600; padding: 6px 12px; display: flex; gap: 10px; align-items: center; }

/* ---------- properties ---------- */
.props-body { padding: 4px 10px 20px; }
.p-head h3 { margin: 6px 0 2px; font-size: 15px; }
.p-desc { margin: 0 0 6px; color: var(--fg2); font-size: 12px; }
.field { display: flex; flex-direction: column; gap: 2px; margin: 6px 0; }
.f-label { font-size: 12px; color: var(--fg2); }
.f-hint { font-size: 11px; color: var(--fg2); }
.f-vwrap { display: flex; align-items: center; gap: 6px; }
.f-value, .f-text, select { background: var(--bg); border: 1px solid var(--line); border-radius: 5px; padding: 4px 7px; min-width: 0; }
.f-value { width: 110px; }
.f-value.small { width: 64px; }
.f-value.bad { border-color: var(--danger); }
.f-parsed { font-size: 11px; color: var(--accent); }
.f-text { width: 100%; }
.f-text.mono, textarea.f-text { font-family: ui-monospace, monospace; font-size: 12px; }
.f-row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; margin: 4px 0; }
.f-livewrap { display: flex; flex-direction: column; gap: 4px; }
.f-slider { width: 100%; accent-color: var(--accent2); }
.f-code textarea { min-height: 150px; }
.p-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
details.adv { margin: 8px 0; }
details.adv summary { cursor: pointer; color: var(--fg2); font-size: 12px; }
.sheet-list { display: flex; flex-wrap: wrap; gap: 4px; }
.bands { display: inline-flex; gap: 3px; margin-top: 3px; }
.band { width: 8px; height: 16px; border-radius: 2px; border: 1px solid rgba(0,0,0,.35); }
.b-black { background: #111; } .b-brown { background: #7b3f00; } .b-red { background: #d11; } .b-orange { background: #f80; } .b-yellow { background: #fd0; } .b-green { background: #2a2; }
.b-blue { background: #22c; } .b-violet { background: #82c; } .b-grey { background: #888; } .b-white { background: #fff; } .b-gold { background: #c9a227; } .b-silver { background: #c0c0c0; }
hr { border: 0; border-top: 1px solid var(--line); margin: 12px 0; }
.chk { display: inline-flex; gap: 5px; align-items: center; font-size: 12px; margin: 2px 8px 2px 0; }

/* ---------- results dock ---------- */
.dock-tabs { display: flex; gap: 2px; padding: 3px 6px 0; border-bottom: 1px solid var(--line); overflow-x: auto; flex: none; }
.dock-tab { background: transparent; border: 0; border-bottom: 2px solid transparent; padding: 5px 10px; cursor: pointer; color: var(--fg2); white-space: nowrap; }
.dock-tab.on { color: var(--fg); border-bottom-color: var(--accent); }
.badge { background: var(--danger); color: #fff; border-radius: 9px; font-size: 10px; padding: 0 5px; margin-left: 4px; }
.dock-body { flex: 1; min-height: 0; position: relative; }
.dock-pane { position: absolute; inset: 0; overflow: auto; padding: 6px 8px; }
.dock-pane[data-tab="plot"]:not([hidden]) { display: flex; gap: 8px; }
.empty { color: var(--fg2); padding: 10px; }
.plot-side { width: 170px; flex: none; overflow: auto; }
.plot-main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.plot-tools { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; padding: 2px 0 4px; }
.plot-holder { flex: 1; min-height: 160px; position: relative; }
.plot-holder.half { height: 46%; min-height: 120px; flex: none; }
.plot { position: absolute; inset: 0; --plot-bg: var(--plot-bg); }
.plot-canvas { width: 100%; height: 100%; display: block; outline: none; }
.plot-legend { position: absolute; top: 4px; right: 8px; display: flex; flex-wrap: wrap; gap: 4px; max-width: 60%; justify-content: flex-end; }
.leg { background: rgba(0,0,0,.25); border: 0; border-radius: 4px; font-size: 11px; padding: 1px 6px; cursor: pointer; color: var(--plot-fg); display: inline-flex; gap: 4px; align-items: center; }
.leg.off { opacity: .4; text-decoration: line-through; }
.sw { width: 10px; height: 3px; display: inline-block; }
.plot-readout { position: absolute; left: 70px; top: 6px; font: 11px ui-monospace, monospace; color: var(--plot-fg); background: rgba(0,0,0,.35); padding: 2px 6px; border-radius: 4px; pointer-events: none; }
.plot-readout:empty { display: none; }
.trace-pick { display: flex; flex-direction: column; }
.scope { display: grid; grid-template-columns: 1fr 260px; grid-template-rows: 1fr auto; gap: 8px; height: 100%; }
.scope-screen { border: 6px solid #1f2a22; border-radius: 10px; background: #071510; min-height: 180px; }
.scope-canvas { width: 100%; height: 100%; display: block; }
.scope-ctl { display: flex; flex-direction: column; gap: 6px; font-size: 12px; }
.scope-ctl label { display: flex; justify-content: space-between; gap: 6px; align-items: center; }
.ch-ctl { border-left: 3px solid var(--ch); padding-left: 6px; }
.meas { border-collapse: collapse; font: 12px ui-monospace, monospace; grid-column: 1 / -1; }
.meas th, .meas td { border-bottom: 1px solid var(--line); padding: 2px 8px; text-align: right; }
.meas th { color: var(--fg2); font-weight: 500; }
.meas.wide td:first-child { text-align: left; font-family: Inter, system-ui, sans-serif; }
.bode { display: flex; flex-direction: column; height: 100%; gap: 4px; }
.bode-info { font-size: 12px; }
.la-canvas { width: 100%; display: block; }
.serial-out { background: var(--plot-bg); border: 1px solid var(--line); border-radius: 6px; padding: 8px; height: calc(100% - 46px); overflow: auto; margin: 0; font: 12px ui-monospace, monospace; white-space: pre-wrap; }
pre.code { background: var(--plot-bg); border: 1px solid var(--line); border-radius: 6px; padding: 8px; font: 12px ui-monospace, monospace; white-space: pre; overflow: auto; max-height: 280px; }
pre.code.small { font-size: 11px; }
.msgs { list-style: none; padding: 0; margin: 0; }
.msgs li { padding: 5px 8px; border-left: 3px solid var(--danger); margin: 4px 0; background: var(--bg3); cursor: pointer; }
.msgs li.warning { border-color: var(--warn); }
.msgs li.info { border-color: var(--accent); }
.warn { color: var(--warn); font-size: 12px; padding: 4px; }

/* ---------- dialogs & toasts ---------- */
.dlg-back { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; z-index: 100; padding: 16px; }
.dlg { background: var(--bg2); border: 1px solid var(--line); border-radius: 12px; width: min(460px, 100%); max-height: 90vh; display: flex; flex-direction: column; box-shadow: 0 16px 50px rgba(0,0,0,.45); }
.dlg.wide { width: min(880px, 100%); }
.dlg-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.dlg-head h2 { margin: 0; font-size: 16px; }
.dlg-body { padding: 10px 14px; overflow: auto; }
.dlg-buttons { display: flex; justify-content: flex-end; gap: 8px; padding: 10px 14px; border-top: 1px solid var(--line); }
.or { text-align: center; color: var(--fg2); margin: 6px 0; }
.ex-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 8px; margin-top: 8px; }
.ex-card { text-align: left; background: var(--bg3); border: 1px solid var(--line); border-radius: var(--radius); padding: 9px 10px; cursor: pointer; display: flex; flex-direction: column; gap: 4px; }
.ex-card:hover { border-color: var(--accent); }
.ex-title { font-weight: 600; }
.ex-text { font-size: 12px; color: var(--fg2); display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.ex-mode { font-size: 11px; color: var(--accent2); }
.file-list { display: flex; flex-direction: column; gap: 3px; margin-bottom: 10px; }
.file-row { display: flex; gap: 10px; align-items: center; }
.linkish { background: none; border: 0; color: var(--accent); cursor: pointer; padding: 3px 0; text-align: left; }
.ai-out { margin-top: 10px; font-size: 13px; line-height: 1.5; }
.ai-out p { margin: 4px 0; }
#toasts { position: fixed; bottom: 34px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 6px; z-index: 200; align-items: center; }
.toast { background: var(--bg3); border: 1px solid var(--line); border-left: 4px solid var(--accent); padding: 7px 14px; border-radius: 6px; box-shadow: 0 6px 18px rgba(0,0,0,.3); max-width: 90vw; transition: opacity .3s; }
.toast.error { border-left-color: var(--danger); } .toast.warn { border-left-color: var(--warn); } .toast.ok { border-left-color: var(--accent); }
.toast.out { opacity: 0; }

/* ---------- phones (viewer) ---------- */
@media (max-width: 1100px) { .app { grid-template-columns: 210px minmax(0, 1fr) 250px; } }
@media (max-width: 640px) {
  .app { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr) 6px 42vh auto auto; grid-template-areas: "top" "center" "grip" "dock" "status" "pbar"; }
  .lib, .props, .toolstrip, .tb-run, .menu, .topbar > .menu-btn, .ai-btn, .b-hi { display: none; }
  .topbar { padding: 4px 8px; }
  .phone-bar { display: flex; gap: 8px; padding: 6px 8px calc(6px + env(safe-area-inset-bottom)); background: var(--bg2); border-top: 1px solid var(--line); }
  .phone-bar .btn { flex: 1; padding: 9px; font-size: 15px; }
  .lesson { left: 8px; right: 8px; max-width: none; max-height: 35%; }
  .status { font-size: 11px; gap: 8px; }
  .plot-side { display: none; }
  .scope { grid-template-columns: 1fr; }
  .dock-tab { padding: 6px 8px; }
}
