*, *::before, *::after { box-sizing: border-box; }

html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--ui-font);
  font-size: 13px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  overscroll-behavior: none;
}
button { font-family: inherit; }
[hidden] { display: none !important; }
.hidden-file { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; left: -9999px; }

/* ---------------- boot / error screens ---------------- */

.boot-loader, .boot-error, .fatal, .errbar { font-family: var(--ui-font); }

.boot-loader {
  position: fixed; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px; background: var(--canvas-bg); color: var(--text-dim);
  background-image: radial-gradient(circle at 1px 1px, var(--grid-dot) 1px, transparent 0);
  background-size: 22px 22px;
}
.boot-bar { width: min(220px, 60vw); height: 3px; background: var(--surface-2); border-radius: 3px; overflow: hidden; }
.boot-bar span { display: block; height: 100%; width: 8%; background: var(--accent); transition: width .18s ease; }
.boot-text { margin: 0; font-size: 12px; }

.boot-error, .fatal {
  position: fixed; inset: 0; margin: auto; max-width: 420px; height: max-content;
  padding: 28px; display: flex; flex-direction: column; gap: 12px;
  align-items: flex-start; justify-content: center;
}
.boot-error h1, .fatal h1 { margin: 0; font-size: 18px; color: var(--text); }
.boot-error p, .fatal p { margin: 0; color: var(--text-dim); line-height: 1.5; }
.boot-error button, .fatal button {
  margin-top: 6px; padding: 8px 16px; border: 0; border-radius: var(--radius-sm);
  background: var(--accent); color: var(--on-accent); font-size: 13px; cursor: pointer;
}

.errbar {
  position: fixed; left: 50%; bottom: 40px; transform: translateX(-50%);
  z-index: 9999; display: flex; align-items: center; gap: 10px;
  background: var(--danger); color: #fff; padding: 8px 12px; border-radius: 999px;
  box-shadow: var(--shadow); font-size: 12px;
}
.errbar button { border: 0; background: rgba(255,255,255,.2); color: #fff; border-radius: 999px; padding: 4px 10px; cursor: pointer; }

/* ---------------- shell layout ---------------- */

.shell {
  position: fixed; inset: 0;
  display: grid;
  grid-template-rows: var(--topbar-h) 1fr var(--status-h);
}

.topbar {
  display: flex; align-items: center; gap: 4px;
  padding: 0 8px; background: var(--surface);
  border-bottom: 1px solid var(--border);
  overflow-x: auto; scrollbar-width: none;
}
.topbar::-webkit-scrollbar { display: none; }
.brand { font-weight: 700; font-size: 14px; letter-spacing: .2px; margin: 0 8px 0 2px; white-space: nowrap; }
.spacer { flex: 1 1 auto; }
.sep { width: 1px; align-self: stretch; margin: 8px 4px; background: var(--border); }

.body { display: flex; min-height: 0; position: relative; }

.left, .right {
  background: var(--surface);
  overflow-y: auto;
  flex: 0 0 auto;
}
.left { width: var(--left-w); border-right: 1px solid var(--border); display: flex; flex-direction: column; }
.right { width: var(--right-w); border-left: 1px solid var(--border); padding: 12px; }
.left-collapsed .left { display: none; }

.canvas {
  flex: 1 1 auto; position: relative; overflow: hidden;
  background-color: var(--canvas-bg);
  background-image: radial-gradient(circle at 1px 1px, var(--grid-dot) 1px, transparent 0);
  background-size: var(--grid-step, 10px) var(--grid-step, 10px);
  background-position: var(--grid-ox, 0px) var(--grid-oy, 0px);
  touch-action: none;
}
.no-grid .canvas { background-image: none; }
.shell.panning .canvas, .shell.space-pan .canvas { cursor: grabbing; }
.shell.placing .canvas { cursor: copy; }
.shell[data-mode="wire"] .canvas, .shell[data-mode="arc"] .canvas { cursor: crosshair; }

.stage { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }

/* ---------------- buttons ---------------- */

.icon, .ghost, .primary {
  border: 1px solid transparent; background: transparent; color: var(--text);
  border-radius: var(--radius-sm); cursor: pointer; white-space: nowrap;
  display: inline-flex; align-items: center; justify-content: center;
  transition: background .12s, border-color .12s, opacity .12s;
}
.icon { width: 32px; height: 32px; font-size: 15px; flex: 0 0 auto; }
.ghost { height: 32px; padding: 0 10px; font-size: 12.5px; }
.primary { height: 32px; padding: 0 12px; font-size: 12.5px; background: var(--accent); color: var(--on-accent); }
.icon:hover, .ghost:hover { background: var(--surface-hover); }
.primary:hover { background: var(--accent-hover); }
.icon.active, .ghost.active { background: var(--accent-weak); color: var(--accent); }
.icon:disabled, .ghost:disabled, .primary:disabled { opacity: .35; cursor: default; }
.icon:focus-visible, .ghost:focus-visible, .primary:focus-visible,
input:focus-visible, select:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}

/* ---------------- palette ---------------- */

.palette-search { padding: 10px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface); z-index: 2; }
#paletteSearch {
  width: 100%; height: 32px; padding: 0 10px; font-size: 12.5px;
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--bg); color: var(--text);
}
.palette-list { padding: 6px; }
.palette-empty { padding: 24px 12px; color: var(--text-faint); text-align: center; font-size: 12px; }

.cat-head {
  width: 100%; display: flex; align-items: center; gap: 6px;
  padding: 8px 6px; border: 0; background: transparent; cursor: pointer;
  font-size: 11px; font-weight: 700; letter-spacing: .5px; color: var(--text-dim);
  text-transform: uppercase;
}
.cat-head:hover { color: var(--text); }
.cat-head .caret { transition: transform .12s; font-size: 9px; }
.cat-head:not(.collapsed) .caret { transform: rotate(90deg); }
.cat-count { margin-left: auto; font-weight: 500; color: var(--text-faint); }

.cat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; padding: 2px 4px 10px; }
.pal-item {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 8px 6px 6px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface); cursor: pointer; transition: border-color .12s, background .12s;
}
.pal-item:hover { border-color: var(--accent); background: var(--accent-weak); }
.pal-item:active { transform: translateY(1px); }
.pal-item.active { border-color: var(--accent); background: var(--accent-weak); }
.pal-preview { width: 100%; height: 44px; }
.pal-preview .sym-stroke { fill: none; stroke: var(--sym-stroke); stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
.pal-preview .sym-fill { fill: var(--sym-fill); stroke: var(--sym-fill); }
.pal-preview .sym-text { fill: var(--sym-stroke); font-size: 10px; }
.pal-name { font-size: 10.5px; color: var(--text-dim); text-align: center; line-height: 1.15; }

/* ---------------- canvas symbols ---------------- */

.content { }
.node { cursor: move; }
.node-hit { fill: transparent; pointer-events: all; }
.node .sym-stroke { fill: none; stroke: var(--sym-stroke); stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
.node .sym-fill { fill: var(--sym-fill); stroke: var(--sym-fill); stroke-width: 1; }
.node .sym-text { fill: var(--sym-stroke); font: 11px var(--ui-font); }
.ins-label { fill: var(--text); font: 600 11px var(--ui-font); }
.ins-value { fill: var(--text-dim); font: 10px var(--ui-font); }
.net-label { fill: var(--accent); font: 10px var(--ui-font); }

.node.selected .sym-stroke { stroke: var(--sel); }
.node.selected .sym-fill { fill: var(--sel); stroke: var(--sel); }
.node.placeholder .sym-stroke { stroke: var(--danger); stroke-dasharray: 3 2; }

.wire { cursor: pointer; }
.wire-line { fill: none; stroke: var(--wire); stroke-width: 1.6; }
.wire-hit { fill: none; stroke: transparent; stroke-width: 14; pointer-events: stroke; }
.wire.selected .wire-line { stroke: var(--sel); stroke-width: 2; }
.wire.dangling .wire-line { stroke: var(--danger); stroke-dasharray: 5 3; }
.ep-open { fill: var(--canvas-bg); stroke: var(--danger); stroke-width: 1.5; }

.pin-handle { fill: var(--accent); opacity: 0; pointer-events: all; cursor: crosshair; transition: opacity .1s; }
.node:hover .pin-handle { opacity: .45; }
.pin-handle:hover { opacity: 1; }
.shell[data-mode="wire"] .pin-handle { opacity: .3; }

.arc { cursor: pointer; }
.arc-line { fill: none; stroke: var(--wire); stroke-width: 1.6; }
.arc-hit { fill: none; stroke: transparent; stroke-width: 14; pointer-events: stroke; }
.arc.selected .arc-line { stroke: var(--sel); stroke-width: 2; }

/* ---------------- overlay ---------------- */

.overlay { pointer-events: none; }
.sel-outline { fill: none; stroke: var(--sel); stroke-width: 1; stroke-dasharray: 3 2; opacity: .8; }
.sel-group { fill: none; stroke: var(--sel); stroke-width: 1; stroke-dasharray: 1 3; opacity: .55; }
.sel-group.sel-merged { stroke-dasharray: none; stroke-width: 1.5; opacity: .85; }
.marquee { fill: color-mix(in srgb, var(--accent) 12%, transparent); stroke: var(--accent); stroke-width: 1; }
.guide { stroke: var(--danger); stroke-width: 1; opacity: .8; }
.wire-preview { fill: none; stroke: var(--accent); stroke-width: 1.6; stroke-dasharray: 4 3; }
.wire-anchor { fill: var(--accent); }
.pin-snap { fill: none; stroke: var(--accent); stroke-width: 1.5; }
.arc-preview { fill: none; stroke: var(--accent); stroke-width: 1.6; stroke-dasharray: 4 3; }
.arc-bulge-guide { stroke: var(--accent); stroke-width: 1; stroke-dasharray: 2 2; opacity: .6; }
.arc-handle { pointer-events: all; cursor: pointer; }
.arc-handle-end { fill: var(--surface); stroke: var(--accent); stroke-width: 1.5; }
.arc-handle-end:hover { fill: var(--accent-weak); }
.arc-handle-bulge { fill: var(--accent); stroke: var(--surface); stroke-width: 1.5; cursor: ns-resize; }
.arc-handle-bulge:hover { fill: var(--accent-hover); }

/* resize handles: an invisible, generously-sized hit target (bigger still on
   touch — see limits.ResizeHandleHitRadiusPx/PxTouch) paired with a small,
   constant-size visible square drawn on top with pointer-events disabled, so
   the affordance never looks oversized while still staying easy to grab. */
.resize-handle-hit { fill: transparent; pointer-events: all; }
.resize-handle { fill: var(--surface); stroke: var(--accent); stroke-width: 1.5; pointer-events: none; }
.resize-handle-hit:hover + .resize-handle { fill: var(--accent-weak); }
.resize-handle-hit-nw, .resize-handle-hit-se { cursor: nwse-resize; }
.resize-handle-hit-ne, .resize-handle-hit-sw { cursor: nesw-resize; }
.resize-handle-hit-n, .resize-handle-hit-s { cursor: ns-resize; }
.resize-handle-hit-e, .resize-handle-hit-w { cursor: ew-resize; }

/* ---------------- properties panel ---------------- */

.props-empty { color: var(--text-faint); font-size: 12px; line-height: 1.6; }
.props-title { font-size: 11px; font-weight: 700; letter-spacing: .5px; text-transform: uppercase; color: var(--text-dim); margin: 14px 0 8px; }
.props-title:first-child { margin-top: 0; }
.props-field { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.props-label { flex: 0 0 68px; font-size: 12px; color: var(--text-dim); }
.props-input {
  flex: 1 1 auto; min-width: 0; height: 30px; padding: 0 8px; font-size: 12.5px;
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--bg); color: var(--text);
}
.props-row { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.props-size-row { display: flex; gap: 10px; margin-bottom: 8px; min-width: 0; }
.props-size-row .props-field { flex: 1 1 0; min-width: 0; margin-bottom: 0; }
.props-size-row .props-label { flex: 0 0 auto; }
.props-check { display: flex; align-items: center; gap: 7px; font-size: 12px; color: var(--text); margin-bottom: 8px; cursor: pointer; user-select: none; }
.props-check input[type="checkbox"] { width: 15px; height: 15px; accent-color: var(--accent); cursor: pointer; }
.props-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-bottom: 8px; }
.props-btn {
  flex: 1 1 auto; height: 30px; padding: 0 10px; font-size: 12px;
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text); cursor: pointer; white-space: nowrap;
}
.props-btn:hover { background: var(--surface-hover); border-color: var(--accent); }
.props-btn.danger { color: var(--danger); }
.props-btn.danger:hover { background: var(--danger-weak); border-color: var(--danger); }
.props-warn { font-size: 11.5px; color: var(--warn); background: color-mix(in srgb, var(--warn) 12%, transparent); padding: 8px; border-radius: var(--radius-sm); margin-bottom: 8px; line-height: 1.45; }

/* ---------------- status bar ---------------- */

.statusbar {
  display: flex; align-items: center; gap: 14px; padding: 0 12px;
  background: var(--surface); border-top: 1px solid var(--border);
  font-size: 11.5px; color: var(--text-dim); overflow: hidden; white-space: nowrap;
}
.statusbar .spacer { flex: 1; }
.st-saved.warn, .st-dangling.warn { color: var(--warn); }
.st-dangling { cursor: pointer; }
.st-dangling:hover { text-decoration: underline; }

/* ---------------- banners ---------------- */

.banner {
  position: absolute; left: 12px; right: 12px; top: 12px; z-index: 40;
  display: flex; align-items: center; gap: 10px; padding: 10px 12px;
  border-radius: var(--radius); box-shadow: var(--shadow);
  font-size: 12.5px; transform: translateY(-140%); transition: transform .25s ease;
}
.banner.show { transform: translateY(0); }
.banner.recovery { background: var(--accent-weak); color: var(--text); border: 1px solid var(--accent); }
.banner.quota { background: color-mix(in srgb, var(--warn) 16%, var(--surface)); color: var(--text); border: 1px solid var(--warn); }
.banner-msg { flex: 1 1 auto; line-height: 1.4; }
.banner-btn {
  flex: 0 0 auto; height: 28px; padding: 0 12px; font-size: 12px;
  border: 0; border-radius: var(--radius-sm); background: var(--accent); color: var(--on-accent); cursor: pointer;
}
.banner-btn.ghost { background: transparent; color: var(--text); border: 1px solid var(--border-strong); }

/* ---------------- toasts ---------------- */

.toast-host {
  position: absolute; left: 50%; bottom: 20px; transform: translateX(-50%);
  z-index: 60; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none;
}
.toast {
  background: var(--text); color: var(--surface); padding: 9px 14px; border-radius: 999px;
  font-size: 12px; box-shadow: var(--shadow); opacity: 0; transform: translateY(8px);
  transition: opacity .2s, transform .2s; max-width: 80vw;
}
.toast.in { opacity: .96; transform: translateY(0); }

/* ---------------- context menu ---------------- */

.contextmenu {
  position: fixed; z-index: 80; min-width: 180px; padding: 4px;
  background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: var(--radius); box-shadow: var(--shadow);
}
.ctx-item {
  display: block; width: 100%; text-align: left; padding: 7px 10px; border: 0;
  background: transparent; color: var(--text); font-size: 12.5px; border-radius: var(--radius-sm); cursor: pointer;
}
.ctx-item:hover { background: var(--accent); color: var(--on-accent); }
.ctx-sep { height: 1px; margin: 4px 6px; background: var(--border); }

/* ---------------- modal (repair report) ---------------- */

.modal-back {
  position: fixed; inset: 0; z-index: 90; background: rgba(15,23,42,.35);
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.modal {
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  max-width: 460px; width: 100%; max-height: 80vh; overflow: auto; padding: 20px;
}
.modal-title { font-size: 15px; font-weight: 700; margin-bottom: 10px; }
.modal-list { margin: 0 0 16px; padding-left: 18px; font-size: 12.5px; color: var(--text-dim); line-height: 1.6; }

/* ---------------- keyboard shortcuts panel ---------------- */

.modal-wide { max-width: 720px; }
.shortcuts-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.shortcuts-head .modal-title { margin-bottom: 0; }
.shortcuts-grid {
  display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 4px 28px;
  margin: 10px 0 14px;
}
.shortcuts-cat-title {
  font-size: 11px; font-weight: 700; letter-spacing: .5px; text-transform: uppercase;
  color: var(--text-dim); margin: 14px 0 6px;
}
.shortcuts-cat:first-child .shortcuts-cat-title,
.shortcuts-cat:nth-child(2) .shortcuts-cat-title { margin-top: 0; }
.shortcuts-row {
  display: flex; align-items: baseline; justify-content: space-between; gap: 12px;
  padding: 3px 0; font-size: 12.5px;
}
.shortcuts-desc { color: var(--text-dim); text-align: right; }
.kbd {
  flex: 0 0 auto; font: 11px/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  background: var(--surface-2); border: 1px solid var(--border-strong); border-radius: 5px;
  padding: 1px 6px; color: var(--text); white-space: nowrap;
}
.shortcuts-foot { font-size: 11.5px; color: var(--text-faint); border-top: 1px solid var(--border); padding-top: 10px; }

@media (max-width: 640px) {
  .shortcuts-grid { grid-template-columns: 1fr; }
}

/* ---------------- export popup ---------------- */

.export-pop {
  z-index: 80; min-width: 160px; padding: 4px; background: var(--surface);
  border: 1px solid var(--border-strong); border-radius: var(--radius); box-shadow: var(--shadow);
}
.export-opt {
  display: block; width: 100%; text-align: left; padding: 8px 10px; border: 0;
  background: transparent; color: var(--text); font-size: 12.5px; border-radius: var(--radius-sm); cursor: pointer;
}
.export-opt:hover { background: var(--accent); color: var(--on-accent); }

/* ---------------- mobile dock + sheet ---------------- */

.mobile-dock {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
  display: flex; gap: 6px; padding: 8px calc(8px + env(safe-area-inset-right)) calc(8px + env(safe-area-inset-bottom)) calc(8px + env(safe-area-inset-left));
  background: var(--surface); border-top: 1px solid var(--border);
}
.dock-btn {
  flex: 1 1 auto; min-height: 44px; border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm); background: var(--surface); color: var(--text);
  font-size: 13px; cursor: pointer;
}
.dock-btn.primary { background: var(--accent); color: var(--on-accent); border-color: var(--accent); flex: 2 1 auto; }
.dock-btn.active { background: var(--accent-weak); color: var(--accent); }

.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 70;
  background: var(--surface); border-top-left-radius: 16px; border-top-right-radius: 16px;
  box-shadow: var(--shadow); max-height: 70vh; overflow: auto;
  transform: translateY(100%); transition: transform .22s ease;
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
}
.sheet.open { transform: translateY(0); }
.sheet-head { display: flex; align-items: center; padding: 14px 16px 8px; }
.sheet-title { font-weight: 700; font-size: 14px; flex: 1; }
.sheet-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; padding: 8px 16px 16px; }
.sheet-cell {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  min-height: 76px; padding: 8px 4px; border: 1px solid var(--border);
  border-radius: var(--radius-sm); background: var(--surface); cursor: pointer;
}
.sheet-cell:active { background: var(--accent-weak); }
.sheet-cell .pal-preview { height: 40px; }
.sheet-name { font-size: 10px; color: var(--text-dim); text-align: center; }

.mobile .left, .mobile .right { display: none; }
.mobile .canvas { padding-bottom: 0; }
.mobile .body { padding-bottom: 60px; }
.mobile .statusbar { display: none; }
.mobile .topbar .sep + .mode ~ * { }

@media (max-width: 820px) {
  .topbar { gap: 2px; }
  .brand { display: none; }
  .icon { width: 38px; height: 38px; }
}

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