:root {
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
  --ease-quart: cubic-bezier(0.16, 1, 0.3, 1);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);

  /* strict neutral technical palette */
  --bg: #101010;
  --surface: #171717;
  --surface-2: #1d1d1d;
  --surface-3: #252525;
  --line: #343434;
  --line-soft: #282828;
  --ink: #d9d9d9;
  --muted: #8c8c8c;
  --muted-2: #626262;
  --amber: #d5d5d5;
  --amber-soft: rgba(213, 213, 213, 0.10);
  --cyan: #aeb8c2;
  --cyan-soft: rgba(174, 184, 194, 0.10);
  --green: #c5cbc5;
  --red: #b26b6b;
  --violet: #a3a3ab;
  --mono: "IBM Plex Mono", "Courier New", ui-monospace, monospace;
  --sans: "IBM Plex Mono", "Courier New", ui-monospace, monospace;
}

* , *::before, *::after {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: var(--tg-viewport-stable-height, 100dvh);
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
  user-select: none;
}

input, textarea, [contenteditable="true"] { user-select: text; font-size: 16px; }

[hidden] { display: none !important; }

button { font-family: inherit; color: inherit; }

#app { display: flex; flex-direction: column; width: 100%; height: 100%; overflow: hidden; }

.svg-defs { position: absolute; width: 0; height: 0; overflow: hidden; }
svg { fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: square; stroke-linejoin: miter; }

/* ---------------------------------------------------------------- header */
.app-header {
  flex-shrink: 0;
  padding: calc(8px + var(--safe-top)) 10px 8px 12px;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  z-index: 10;
}
.brand { display: flex; align-items: center; gap: 9px; min-width: 0; }
.brand__mark {
  width: 30px; height: 30px; flex-shrink: 0;
  display: grid; place-items: center;
  color: var(--ink);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 0;
}
.brand__mark svg { width: 18px; height: 18px; }
.brand__text { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.brand__title {
  font-size: 12px; font-weight: 600; letter-spacing: 0.16em; text-transform: uppercase;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.mode-badge {
  display: inline-flex; align-items: center; gap: 6px;
  font: 500 9px/1 var(--mono); letter-spacing: .10em; text-transform: uppercase; color: var(--muted);
}
.mode-badge .dot, .sim-status__dot {
  width: 5px; height: 5px; border-radius: 0; background: var(--muted-2); flex-shrink: 0;
}
.mode-badge.is-live .dot, .sim-status.is-live .sim-status__dot { background: var(--ink); }
.mode-badge.is-warn .dot, .sim-status.is-warn .sim-status__dot { background: var(--amber); }
.mode-badge.is-error .dot, .sim-status.is-error .sim-status__dot { background: var(--red); }

.header-actions { display: flex; align-items: center; gap: 4px; flex-shrink: 0; }

.icon-btn {
  width: 34px; height: 34px; flex-shrink: 0;
  display: grid; place-items: center;
  border: 1px solid var(--line);
  border-radius: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  transition: color 140ms var(--ease-out), background 140ms var(--ease-out);
}
.icon-btn svg { width: 16px; height: 16px; }
.icon-btn:disabled { opacity: .32; pointer-events: none; }
.icon-btn--sm { width: 28px; height: 28px; }
.icon-btn--sm svg { width: 14px; height: 14px; }
.icon-btn--danger { color: var(--red); }
@media (hover: hover) { .icon-btn:hover:not(:disabled) { color: var(--ink); background: var(--surface-2); } }

.run-btn {
  height: 34px; padding: 0 12px; display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--ink); border-radius: 0;
  background: var(--ink); color: #111111;
  font: 600 10px/1 var(--mono); letter-spacing: .10em; text-transform: uppercase;
  cursor: pointer;
  transition: transform 90ms var(--ease-out), opacity 140ms var(--ease-out), background 140ms var(--ease-out);
}
.run-btn svg { width: 13px; height: 13px; stroke-width: 2; }
.run-btn:disabled { opacity: .32; }
.run-btn.is-running { background: transparent; color: var(--ink); border-color: var(--line); }
.run-btn:active:not(:disabled) { transform: scale(.975); }

.app-viewport { flex: 1; position: relative; min-height: 0; overflow: hidden; }
.view {
  position: absolute; inset: 0; overflow-y: auto; overflow-x: hidden;
  -webkit-overflow-scrolling: touch; overscroll-behavior-y: contain; scrollbar-gutter: stable;
}
.view[hidden] { display: none; }
.view__pad { padding: 10px 12px 24px; display: flex; flex-direction: column; gap: 10px; }

@keyframes paneFromRight { from { opacity: 0; transform: translate3d(14px,0,0); } to { opacity: 1; transform: none; } }
@keyframes paneFromLeft  { from { opacity: 0; transform: translate3d(-14px,0,0); } to { opacity: 1; transform: none; } }
.view.enter-right > .view__pad, .view.enter-right > .editor-shell { animation: paneFromRight 200ms var(--ease-out) both; }
.view.enter-left  > .view__pad, .view.enter-left  > .editor-shell { animation: paneFromLeft 200ms var(--ease-out) both; }

/* ---------------------------------------------------------------- editor */
.editor-shell { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.toolbar {
  flex-shrink: 0; display: flex; align-items: center; gap: 4px;
  padding: 6px 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  overflow-x: auto; scrollbar-width: none;
}
.toolbar::-webkit-scrollbar { display: none; }
.tool-group { display: flex; gap: 2px; }
.toolbar__divider { width: 1px; height: 20px; background: var(--line); margin: 0 4px; flex-shrink: 0; }
.tool {
  height: 30px; padding: 0 8px; display: inline-flex; align-items: center; gap: 5px;
  border: 1px solid transparent; border-radius: 0;
  background: none; color: var(--muted);
  font: 500 10px/1 var(--mono); letter-spacing: .04em; white-space: nowrap; cursor: pointer;
  transition: color 140ms var(--ease-out), background 140ms var(--ease-out), border-color 140ms var(--ease-out);
}
.tool svg { width: 15px; height: 15px; }
.tool.is-active { color: var(--ink); background: var(--surface-3); border-color: var(--line); }
@media (hover: hover) { .tool:hover { color: var(--ink); } }
.zoom-group { display: flex; gap: 2px; }
.values-toggle.is-on { color: var(--ink); border-color: var(--line); background: var(--surface-2); }

.stage { position: relative; flex: 1; min-height: 0; overflow: hidden; background: #0e0e0e; }
#canvas { display: block; width: 100%; height: 100%; touch-action: none; cursor: crosshair; }
#canvas.tool-select { cursor: default; }
#canvas.tool-erase { cursor: not-allowed; }

.sim-status {
  position: absolute; top: 8px; left: 8px; z-index: 3;
  display: inline-flex; align-items: center; gap: 6px;
  padding: 5px 8px; border: 1px solid var(--line); border-radius: 0;
  background: var(--surface);
  font: 500 9px/1 var(--mono); letter-spacing: .08em; text-transform: uppercase; color: var(--muted);
  pointer-events: none;
}
.canvas-hint {
  position: absolute; top: 8px; right: 8px; z-index: 3; max-width: 70%;
  padding: 6px 8px; border: 1px solid var(--line); border-radius: 0;
  background: var(--surface); color: var(--ink);
  font: 500 10px/1.4 var(--sans);
}
.empty-state {
  position: absolute; inset: 0; z-index: 2;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; padding: 24px; gap: 7px; background: rgba(14,14,14,.94);
}
.empty-state__icon {
  width: 48px; height: 48px; display: grid; place-items: center;
  color: var(--muted); border: 1px dashed var(--line); border-radius: 0; margin-bottom: 4px;
}
.empty-state__icon svg { width: 24px; height: 24px; }
.empty-state__title { margin: 0; font-size: 14px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; }
.empty-state__text { margin: 0; max-width: 300px; font-size: 11px; line-height: 1.6; color: var(--muted); }
.empty-state__actions { display: flex; gap: 6px; margin-top: 7px; }

.btn {
  height: 34px; padding: 0 12px;
  border: 1px solid var(--line); border-radius: 0;
  background: transparent; color: var(--ink);
  font: 500 10px/1 var(--mono); letter-spacing: .06em; text-transform: uppercase; cursor: pointer;
  transition: transform 90ms var(--ease-out), background 140ms var(--ease-out), border-color 140ms var(--ease-out);
}
.btn:active { transform: scale(.98); }
.btn--primary { background: var(--ink); border-color: var(--ink); color: #111111; }
.btn--ghost { background: none; }
.btn--danger { color: var(--red); border-color: #4a3232; }
.btn--sm { height: 28px; padding: 0 9px; font-size: 9px; }
.btn--block { width: 100%; }
.btn:disabled { opacity: .35; pointer-events: none; }
@media (hover: hover) { .btn:hover:not(:disabled) { border-color: var(--muted); } .btn--primary:hover { background: #eeeeee; } }

.selection-card {
  position: absolute; left: 8px; right: 8px; bottom: 8px; z-index: 4;
  display: flex; flex-direction: column; gap: 7px;
  padding: 8px 10px;
  border: 1px solid var(--line); border-radius: 0;
  background: var(--surface);
}
.selection-card__head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.selection-card__name { font-size: 11px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; }
.selection-card__readout { font: 500 10px/1.2 var(--mono); color: var(--ink); text-align: right; }
.selection-card__actions { display: flex; align-items: center; gap: 4px; }

/* ---------------------------------------------------------------- canvas items */
.comp-body { cursor: pointer; }
.comp-body .wire-lead { stroke: #666666; stroke-width: 1.8; fill: none; }
.comp-body .sym { stroke: var(--ink); stroke-width: 1.6; fill: none; }
.comp-body .sym-fill { fill: var(--surface-2); stroke: var(--ink); stroke-width: 1.6; }
.comp-body .box-fill { fill: #171717; stroke: #6a6a6a; stroke-width: 1.2; }
.comp-body .box-accent { fill: #1f1f1f; stroke: #a8a8a8; stroke-width: 1.1; }
svg text { stroke: none; paint-order: fill; }

.comp-body text { fill: #8f8f8f; stroke: none; font-family: var(--mono); font-size: 9px; font-weight: 400; }
.comp.selected .comp-body .sym, .comp.selected .comp-body .sym-fill { stroke: #ffffff; }
.comp.selected .comp-body .box-fill, .comp.selected .comp-body .box-accent { stroke: #ffffff; }
.comp-label { fill: var(--muted); stroke: none; font-family: var(--mono); font-size: 9px; font-weight: 400; text-anchor: middle; pointer-events: none; }
.comp-readout { fill: #bdbdbd; stroke: none; font-family: var(--mono); font-size: 8.5px; font-weight: 400; text-anchor: middle; pointer-events: none; font-variant-numeric: tabular-nums; }
.comp-readout.is-warn { fill: var(--amber); }

.pin { cursor: crosshair; }
.pin .pin-dot { fill: #6a6a6a; stroke: none; }
.pin .pin-hit { fill: transparent; stroke: none; }
.pin:hover .pin-dot, .pin.is-target .pin-dot { fill: #ffffff; }
.pin.is-target .pin-dot { r: 4.6px; }
.pin.is-source .pin-dot { fill: #ffffff; }

.wire-path { fill: none; stroke: #666666; stroke-width: 1.6; stroke-linejoin: miter; stroke-linecap: square; cursor: pointer; }
.wire-hit { fill: none; stroke: transparent; stroke-width: 14; cursor: pointer; }
.wire.selected .wire-path { stroke: #ffffff; stroke-width: 2; }
.wire-preview { fill: none; stroke: #ffffff; stroke-width: 1.4; stroke-dasharray: 5 4; pointer-events: none; opacity: .8; }

/* ---------------------------------------------------------------- panels */
.panel {
  border: 1px solid var(--line); border-radius: 0;
  background: var(--surface); padding: 10px;
  display: flex; flex-direction: column; gap: 9px;
}
.panel__head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.panel__title { margin: 0; font-size: 12px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; }
.panel__sub { margin: 3px 0 0; font-size: 10px; line-height: 1.55; color: var(--muted); }

.probe-picker { display: flex; flex-direction: column; gap: 8px; }
.probe-group__title { font: 600 9px/1 var(--mono); letter-spacing: .10em; color: var(--muted-2); text-transform: uppercase; margin: 2px 0; }
.probe-chips { display: flex; flex-wrap: wrap; gap: 4px; }
.probe-chip {
  display: inline-flex; align-items: center; gap: 5px;
  height: 28px; padding: 0 8px;
  border: 1px solid var(--line); border-radius: 0;
  background: transparent; color: var(--muted);
  font: 500 9px/1 var(--mono); cursor: pointer;
  transition: color 140ms var(--ease-out), border-color 140ms var(--ease-out), background 140ms var(--ease-out);
}
.probe-chip .swatch { width: 6px; height: 6px; border-radius: 0; background: var(--muted-2); }
.probe-chip.is-on { color: var(--ink); border-color: var(--muted); background: var(--surface-2); }
.probe-chip.is-on .swatch { background: var(--ink); }

.chart-panel { padding: 7px; gap: 7px; }
.scope-canvas { width: 100%; height: 260px; display: block; border-radius: 0; background: #0b0b0b; border: 1px solid var(--line-soft); }
.legend { display: flex; flex-wrap: wrap; gap: 5px 12px; padding: 2px 3px 3px; }
.legend__item { display: inline-flex; align-items: center; gap: 5px; font: 500 9px/1 var(--mono); color: var(--muted); }
.legend__dot { width: 6px; height: 6px; border-radius: 0; }
.legend__val { color: var(--ink); }

/* ---------------------------------------------------------------- library */
.project-list { display: flex; flex-direction: column; gap: 4px; }
.project-row {
  display: flex; align-items: center; gap: 8px;
  padding: 8px; border: 1px solid var(--line-soft); border-radius: 0;
  background: var(--surface-2);
}
.project-row__main { flex: 1; min-width: 0; }
.project-row__name { font-size: 11px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.project-row__meta { margin-top: 2px; font: 500 9px/1 var(--mono); color: var(--muted-2); }
.project-row__actions { display: flex; gap: 3px; }
.empty-note { padding: 12px; text-align: center; color: var(--muted); font-size: 10px; border: 1px dashed var(--line); border-radius: 0; }

.example-grid { display: grid; grid-template-columns: 1fr; gap: 5px; }
.example-card {
  text-align: left; padding: 9px 10px;
  border: 1px solid var(--line-soft); border-radius: 0;
  background: var(--surface-2); cursor: pointer;
  transition: transform 90ms var(--ease-out), border-color 140ms var(--ease-out);
}
.example-card:active { transform: scale(.99); }
.example-card__tag { font: 600 8px/1 var(--mono); letter-spacing: .12em; text-transform: uppercase; color: var(--muted); }
.example-card__name { margin-top: 4px; font-size: 12px; font-weight: 600; }
.example-card__note { margin-top: 3px; font-size: 10px; line-height: 1.5; color: var(--muted); }
@media (min-width: 700px) {
  .example-grid { grid-template-columns: 1fr 1fr; }
  .view__pad { padding: 14px 18px 28px; max-width: 1100px; margin: 0 auto; width: 100%; }
  .brand__title { font-size: 13px; }
}

/* ---------------------------------------------------------------- tabbar */
.tabbar {
  flex-shrink: 0; position: relative;
  display: grid; grid-template-columns: repeat(var(--tabs, 3), 1fr);
  padding: 0 0 max(4px, var(--safe-bottom));
  background: var(--surface);
  border-top: 1px solid var(--line);
  z-index: 20;
}
.tabbar__pill { display: none; }
.tab {
  position: relative; z-index: 1; height: 44px; border: 0; background: none;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  font: 500 9px/1 var(--sans); letter-spacing: .08em; text-transform: uppercase; color: var(--muted);
  cursor: pointer; border-top: 2px solid transparent;
  transition: color 160ms var(--ease-out), border-color 160ms var(--ease-out);
}
.tab svg { width: 16px; height: 16px; }
.tab.is-active { color: var(--ink); border-top-color: var(--ink); }
body.kb-open .tabbar { display: none; }

/* ---------------------------------------------------------------- sheet */
.backdrop {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(0,0,0,.72);
  opacity: 0; pointer-events: none;
  transition: opacity 180ms var(--ease-out);
}
.backdrop.is-open { opacity: 1; pointer-events: auto; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 51;
  max-height: 88%; display: flex; flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--line); border-bottom: 0; border-radius: 0;
  padding: 6px 12px calc(10px + var(--safe-bottom));
  transform: translate3d(0, 100%, 0);
  transition: transform 280ms var(--ease-quart);
  will-change: transform;
}
.sheet.is-open { transform: translate3d(0,0,0); }
.sheet__handle { width: 32px; height: 2px; border-radius: 0; background: var(--line); margin: 4px auto 7px; flex-shrink: 0; }
.sheet__head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding-bottom: 7px; border-bottom: 1px solid var(--line-soft); }
.sheet__title { margin: 0; font-size: 12px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; }
.sheet__body { overflow-y: auto; overscroll-behavior: contain; display: flex; flex-direction: column; gap: 10px; padding-top: 9px; }
.sheet__foot { display: flex; gap: 5px; padding-top: 10px; border-top: 1px solid var(--line-soft); }
.sheet__foot .btn { flex: 1; }

.search-field {
  display: flex; align-items: center; gap: 8px;
  height: 36px; padding: 0 10px;
  border: 1px solid var(--line); border-radius: 0; background: var(--surface-2);
}
.search-field svg { width: 14px; height: 14px; color: var(--muted); flex-shrink: 0; }
.search-field input { flex: 1; border: 0; background: none; outline: none; color: var(--ink); font-size: 16px; font-family: var(--mono); }

.catalog-group { display: flex; flex-direction: column; gap: 5px; }
.catalog-group__title { font: 600 9px/1 var(--mono); letter-spacing: .10em; text-transform: uppercase; color: var(--muted-2); }
.catalog-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 4px; }
@media (min-width: 560px) { .catalog-grid { grid-template-columns: repeat(3, 1fr); } }
.catalog-item {
  display: flex; align-items: center; gap: 7px;
  min-height: 40px; padding: 5px 7px;
  border: 1px solid var(--line-soft); border-radius: 0;
  background: var(--surface-2); text-align: left; cursor: pointer;
}
.catalog-item__icon { width: 26px; height: 22px; flex-shrink: 0; display: grid; place-items: center; color: var(--muted); }
.catalog-item__icon svg { width: 24px; height: 20px; }
.catalog-item__name { font-size: 10px; font-weight: 500; line-height: 1.3; }
.catalog-item:active { border-color: var(--ink); }

.field { display: flex; flex-direction: column; gap: 4px; }
.field__label { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; font-size: 10px; color: var(--muted); }
.field__unit { font: 500 9px/1 var(--mono); color: var(--muted-2); }
.field input[type="text"], .field input[type="number"], .field select, .field textarea {
  width: 100%; height: 34px; padding: 0 9px;
  border: 1px solid var(--line); border-radius: 0;
  background: var(--surface-2); color: var(--ink);
  font: 500 14px/1 var(--mono); outline: none;
}
.field textarea { height: 64px; padding: 7px 9px; line-height: 1.4; resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--muted); }
.field--row { flex-direction: row; align-items: center; justify-content: space-between; }
.field--row .field__label { flex: 1; }
.switch {
  position: relative; width: 38px; height: 22px; flex-shrink: 0;
  border: 1px solid var(--line); border-radius: 0; background: var(--surface-3);
  cursor: pointer; transition: background 140ms var(--ease-out), border-color 140ms var(--ease-out);
}
.switch::after {
  content: ""; position: absolute; top: 2px; left: 2px;
  width: 16px; height: 16px; border-radius: 0; background: var(--muted);
  transition: transform 160ms var(--ease-out), background 160ms var(--ease-out);
}
.switch.is-on { background: var(--surface-2); border-color: var(--muted); }
.switch.is-on::after { transform: translate3d(16px,0,0); background: var(--ink); }
.seg { position: relative; display: inline-flex; width: 100%; padding: 2px; border-radius: 0; background: var(--surface-3); border: 1px solid var(--line); }
.seg__btn { flex: 1; height: 28px; border: 0; background: none; color: var(--muted); font: 600 10px/1 var(--sans); border-radius: 0; cursor: pointer; }
.seg__btn.is-active { background: var(--surface); color: var(--ink); }
.btn-row { display: flex; gap: 5px; }

/* ---------------------------------------------------------------- toast & boot */
.toast {
  position: fixed; top: calc(8px + var(--safe-top)); left: 50%; z-index: 80;
  max-width: 88vw; padding: 8px 12px; border: 1px solid var(--ink); border-radius: 0;
  background: var(--surface); color: var(--ink);
  font: 500 10px/1.4 var(--mono); text-align: center;
  opacity: 0; transform: translate3d(-50%,-12px,0);
  pointer-events: none;
  transition: opacity 160ms var(--ease-out), transform 180ms var(--ease-out);
}
.toast.is-shown { opacity: 1; transform: translate3d(-50%,0,0); }
.toast.is-error { border-color: var(--red); color: var(--red); }
.boot {
  position: fixed; inset: 0; z-index: 90;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  background: var(--bg); color: var(--muted);
  transition: opacity 240ms var(--ease-out);
}
.boot.is-done { opacity: 0; pointer-events: none; }
.boot__spinner {
  width: 20px; height: 20px; border-radius: 0;
  border: 2px solid var(--line); border-top-color: var(--ink);
  animation: spin 800ms linear infinite;
}
.boot__text { font: 500 9px/1 var(--mono); letter-spacing: .10em; text-transform: uppercase; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
