/* Sticker sheet look: parts are die-cut stickers on a green cutting mat, the controls are white labels */
:root {
  --ink: #1d2b24;
  --ink-soft: #4c5a52;
  --page: #e6e9e2;
  --sheet: #f6f6f1;        /* backing paper of the parts list */
  --mat: #2f7d5b;          /* cutting mat */
  --mat-shadow: rgba(11, 43, 32, .45);
  --accent: #ffd23f;
  --edge: #c5cabf;         /* flat shadow under the labels */
  --danger: #b3261e;
  --font-title: 'Baloo 2', 'Arial Rounded MT Bold', sans-serif;
  --font-text: Nunito, system-ui, sans-serif;
}
* { box-sizing: border-box; text-decoration: none; }
html, body { margin: 0; height: 100%; height: 100dvh; overscroll-behavior: none; font: 500 15px/1.4 var(--font-text); color: var(--ink); }
body { display: grid; grid-template-columns: 300px 1fr; grid-template-rows: 68px 1fr; background: var(--page); }
button, select, input { font: inherit; color: var(--ink); }
a { color: var(--ink); }

/* ---------- controls ---------- */
button, select, .namebox {
  height: 44px; border: 0; border-radius: 22px; padding: 0 18px; background: #fff; box-shadow: 0 2px 0 var(--edge); outline: none;
}
button, select { font: 700 16px/1 var(--font-title); cursor: pointer; transition: background .15s, transform .15s; }
button:hover, select:hover { background: var(--accent); transform: translateY(-1px); }
button:focus-visible, select:focus-visible, input:focus-visible, .toggle:focus-within { outline: 3px solid var(--ink); outline-offset: 2px; }
button.primary { background: var(--ink); color: var(--accent); }
button.primary:hover { background: #000; }
kbd { font: 700 12px var(--font-text); color: var(--ink-soft); }

header { grid-column: 1 / 3; display: flex; align-items: center; gap: 10px; padding: 0 16px; z-index: 2; }
header .logo {
  font: 800 26px/1 var(--font-title); padding: 8px 14px 6px; margin-right: 10px; white-space: nowrap;
  background: var(--accent); border: 4px solid #fff; border-radius: 14px; box-shadow: 2px 3px 0 #bfc5bb; transform: rotate(-2deg);
}
header .grow { flex: 1; }
.namebox { display: flex; align-items: center; gap: 10px; padding: 0 14px; }
.namebox input { width: 170px; height: 100%; border: 0; outline: none; background: none; font-weight: 700; padding: 0; }
#status { font-size: 12px; font-weight: 700; color: var(--ink-soft); white-space: nowrap; }
#status:empty { display: none; }
/* unsaved changes: a dot before the name */
.namebox::before { content: ''; width: 10px; height: 10px; margin-right: -2px; border-radius: 50%; background: var(--mat); }
body.dirty .namebox::before { background: #f58a1f; }

/* wiring check: the round badge says the state, the text says how many */
#btn-check { display: flex; align-items: center; gap: 8px; padding-left: 8px; }
#btn-check::before {
  content: '✓'; display: grid; place-items: center; width: 28px; height: 28px; border-radius: 50%;
  background: var(--mat); color: #fff; font: 700 15px var(--font-text);
}
#btn-check.warn::before { content: '!'; background: #f58a1f; color: var(--ink); }
#btn-check.error::before { content: '!'; background: var(--danger); }

/* ---------- parts list: a sticker sheet ---------- */
aside { display: flex; flex-direction: column; min-height: 0; margin: 0 0 16px 16px; background: var(--sheet); border-radius: 18px; box-shadow: 0 2px 0 var(--edge); }
#search { margin: 14px 14px 10px; width: calc(100% - 28px); height: 44px; padding: 0 16px; border: 2px solid #d5d9cf; border-radius: 22px; background: #fff; outline: none; }
#tags { display: flex; flex-wrap: wrap; gap: 5px; margin: 0 14px 10px; }
.tag { height: 28px; padding: 0 10px; border-radius: 14px; box-shadow: none; font-size: 13px; }
.tag span { font-weight: 600; color: var(--ink-soft); margin-left: 3px; }
.tag.on { background: var(--accent); }
#palette { overflow: auto; flex: 1; padding: 6px 14px 14px; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px 8px; align-content: start; }
.card { padding: 6px 4px; border-radius: 12px; cursor: grab; text-align: center; font: 700 12px/1.2 var(--font-text); transition: background .15s; }
.card:hover { background: #fff; }
.card img {
  width: 100%; height: 76px; padding: 4px; object-fit: contain; pointer-events: none; transition: transform .15s;
  /* die-cut border: the drawing's own outline pushed out in white on four sides, then a flat shadow */
  filter: drop-shadow(1.5px 0 0 #fff) drop-shadow(-1.5px 0 0 #fff) drop-shadow(0 1.5px 0 #fff) drop-shadow(0 -1.5px 0 #fff) drop-shadow(1.5px 2px 0 #bfc5cc);
}
.card:hover img { transform: rotate(-3deg) scale(1.05); }
.card span { display: block; height: 29px; overflow: hidden; margin-top: 2px; }
.credit { margin: 0; padding: 6px 14px 10px; font-size: 11px; line-height: 1.3; color: var(--ink-soft); }
.credit a { text-decoration: underline; }

/* ---------- canvas: the cutting mat ---------- */
main { position: relative; min-width: 0; min-height: 0; margin: 0 16px 16px; border-radius: 18px; overflow: hidden; background: var(--mat); }
#canvas { width: 100%; height: 100%; display: block; cursor: default; touch-action: none; user-select: none; -webkit-user-select: none; }
#canvas.panning { cursor: grabbing; }
#canvas.far #grid-bg { visibility: hidden; }      /* zoomed out: the 0.1" lines would turn into a grey veil */
.part { cursor: move; }
.part .outline { fill: transparent; stroke: none; }
.part.selected .outline { stroke: var(--accent); stroke-width: 1.4; stroke-dasharray: 5 3; fill: rgba(255, 210, 63, .12); }
.pin { fill: transparent; stroke: none; cursor: crosshair; }
/* corner handle of a resizable sticker */
.part-resize { display: none; fill: #fff; stroke: var(--ink); cursor: nwse-resize; }
.part.selected .part-resize { display: block; }
.pin:hover, .pin.target { fill: rgba(255, 210, 63, .75); stroke: #000; stroke-width: .6; }
.erc-mark { fill: none; stroke-width: 2; stroke-dasharray: 3 2; pointer-events: none; }
.erc-mark.error { stroke: #ff6b66; fill: rgba(229, 57, 53, .3); }
.erc-mark.warn { stroke: #ffb347; fill: rgba(245, 138, 31, .3); }
.wire { pointer-events: none; fill: none; stroke-width: 2.6; stroke-linecap: round; stroke-linejoin: round; }
.wire-shadow { fill: none; stroke: rgba(0, 0, 0, .3); stroke-width: 3.8; stroke-linecap: round; stroke-linejoin: round; pointer-events: none; }
.wire-hit { fill: none; stroke: transparent; stroke-width: 8; cursor: pointer; }
.wire-end { stroke: rgba(0, 0, 0, .35); stroke-width: .8; pointer-events: none; }
.wire-sel { fill: none; stroke: var(--accent); stroke-width: 7; opacity: .7; stroke-linecap: round; stroke-linejoin: round; pointer-events: none; }
.handle { fill: #fff; stroke: var(--ink); stroke-width: 1.4; }
.handle.ns { cursor: ns-resize; }
.handle.ew { cursor: ew-resize; }
#wire-preview { fill: none; stroke-width: 2.6; stroke-linecap: round; stroke-linejoin: round; opacity: .85; pointer-events: none; }

/* ---------- notes: label stickers (white border and flat shadow are rects drawn in app.js) ---------- */
.note { cursor: move; }
.note-paper { stroke: #fff; }
.note-shadow { fill: var(--mat-shadow); }
.note-outline { fill: none; stroke: none; pointer-events: none; }
.note.selected .note-outline { stroke: var(--accent); stroke-width: 1.4; stroke-dasharray: 5 3; fill: rgba(255, 210, 63, .12); }
.note-body {
  box-sizing: border-box; width: 100%; height: 100%; display: flex; padding: 7px 9px;
  font: 500 13px/1.3 var(--font-text); color: var(--ink); cursor: move; overflow: hidden;
}
.note.editing .note-body { cursor: text; }
.note-text {
  flex: 1; width: 100%; resize: none; border: 0; outline: none; background: transparent; font: inherit; color: inherit;
  pointer-events: none; padding: 0; overflow: hidden;
}
.note-text::placeholder { color: inherit; opacity: .55; }
.note.editing .note-text { pointer-events: auto; overflow: auto; }
.note-resize-hit { fill: transparent; cursor: nwse-resize; opacity: 0; pointer-events: none; }
.note-resize { fill: #fff; stroke: var(--ink); stroke-width: 1.4; opacity: 0; pointer-events: none; }
.note.selected .note-resize-hit { pointer-events: auto; }
.note.selected .note-resize { opacity: 1; }

/* ---------- floating controls over the mat ---------- */
#seltools, #dock { position: absolute; display: flex; align-items: center; background: #fff; box-shadow: 3px 4px 0 var(--mat-shadow); }
#seltools[hidden] { display: none; }
#seltools button, #dock button, .toggle { height: 40px; border-radius: 20px; box-shadow: none; background: none; }
#seltools button:hover, #dock button:hover { background: var(--page); transform: none; }

/* actions on the selection, placed next to it by placeTools() in app.js */
#seltools { left: 0; top: 0; gap: 2px; padding: 4px; border-radius: 24px; white-space: nowrap; z-index: 1; }
#sel-name { max-width: 190px; overflow: hidden; text-overflow: ellipsis; padding: 0 6px 0 12px; font: 700 14px var(--font-title); }
#sel-name:empty { display: none; }
#seltools button { display: flex; align-items: center; gap: 6px; padding: 0 10px; }
#seltools svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
#seltools .danger, #seltools .danger kbd { color: var(--danger); }
#seltools:not([data-type=part]) .part-only, #seltools[data-type=wire] .no-wire { display: none; }

/* tools that do not depend on the selection */
#dock { left: 50%; bottom: 16px; transform: translateX(-50%); gap: 8px; padding: 6px 8px 6px 16px; border-radius: 28px; max-width: calc(100% - 24px); }
#dock button { padding: 0 14px; font-size: 15px; }
#dock button.soft { background: var(--page); }
#dock button.soft:hover { background: var(--accent); }
.wire-label { font: 700 15px var(--font-title); }
#swatches { display: flex; gap: 5px; }
#dock button.sw { width: 22px; height: 22px; padding: 0; border-radius: 50%; border: 2px solid #fff; box-shadow: 0 0 0 1px rgba(0, 0, 0, .3); }
#dock button.sw:hover { transform: scale(1.2); background-blend-mode: normal; }
#dock button.sw.on { box-shadow: 0 0 0 2px var(--ink); }
.toggle { position: relative; display: flex; align-items: center; padding: 0 16px; font: 700 15px/1 var(--font-title); cursor: pointer; }
.toggle input { position: absolute; opacity: 0; pointer-events: none; }
.toggle:has(input:checked) { background: var(--accent); }
.zoom { display: flex; align-items: center; }
#dock .zoom button { width: 40px; padding: 0; font: 700 20px var(--font-text); }
#zoom { min-width: 46px; text-align: center; font-size: 13px; font-weight: 700; }

#tip { position: absolute; pointer-events: none; background: var(--ink); color: var(--accent); padding: 3px 12px; border-radius: 1em; font-size: 12px; font-weight: 700; white-space: nowrap; z-index: 2; }
#erc { position: absolute; left: 12px; top: 12px; margin: 0; padding: 10px 16px 10px 32px; max-width: min(520px, calc(100% - 24px)); max-height: 40%; overflow: auto;
  background: #fff; border-radius: 16px; box-shadow: 3px 4px 0 var(--mat-shadow); font-size: 13px; font-weight: 700; line-height: 1.5; }
#erc li.error::marker { color: var(--danger); }
#erc li.warn::marker { color: #f58a1f; }

/* ---------- open dialog: a gallery of saved circuits ---------- */
dialog { width: min(1040px, calc(100vw - 32px)); max-height: min(760px, calc(100dvh - 32px)); padding: 0; border: 0; border-radius: 22px; background: var(--sheet); color: var(--ink); overflow: hidden; }
dialog[open] { display: flex; flex-direction: column; }
dialog::backdrop { background: rgba(11, 43, 32, .55); }
.dlg-head { display: flex; align-items: center; gap: 12px; padding: 14px 14px 12px 20px; }
.dlg-head h2 { margin: 0 auto 0 0; font: 800 22px/1 var(--font-title); white-space: nowrap; }
#open-search { flex: 0 1 280px; min-width: 0; height: 44px; padding: 0 16px; border: 2px solid #d5d9cf; border-radius: 22px; background: #fff; outline: none; }
#open-close { flex: none; display: grid; place-items: center; width: 44px; padding: 0; }
#open-close svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; }
#open-grid { overflow: auto; padding: 4px 20px 20px; display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 14px; align-content: start; font-weight: 700; color: var(--ink-soft); }
.shot { position: relative; border-radius: 16px; background: #fff; box-shadow: 0 2px 0 var(--edge); }
.shot-open { display: block; width: 100%; height: auto; padding: 6px 6px 10px; border-radius: 16px; background: none; box-shadow: none; text-align: left; white-space: normal; }
/* 4:3 tile sized by its padding: Safari collapses an aspect-ratio box that has no content of its own inside a button */
.shot .thumb { display: block; position: relative; width: 100%; height: 0; padding-top: 75%; border-radius: 11px; background: var(--mat); }
.shot svg { position: absolute; left: 12px; top: 12px; width: calc(100% - 24px); height: calc(100% - 24px); }
.shot strong { display: block; margin: 9px 8px 0; font: 700 15px/1.2 var(--font-title); overflow-wrap: anywhere; }
.shot small { display: block; margin: 2px 8px 0; font: 500 12px var(--font-text); color: var(--ink-soft); }
.shot-del { position: absolute; top: 12px; right: 12px; display: grid; place-items: center; width: 40px; height: 40px; padding: 0; border-radius: 50%; box-shadow: 2px 3px 0 var(--mat-shadow); color: var(--danger); }
.shot-del svg { position: static; width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.shot-confirm { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; padding: 12px; border-radius: 16px; background: #fff; text-align: center; color: var(--ink); }
.shot-confirm[hidden] { display: none; }
.shot-confirm p { margin: 0; font: 500 13px/1.35 var(--font-text); overflow-wrap: anywhere; }
.shot-confirm b { font: 700 15px/1.2 var(--font-title); }
.shot-confirm .choices { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; }
.shot-confirm button { height: 40px; padding: 0 14px; background: var(--page); box-shadow: none; font-size: 15px; }
.shot-confirm button:disabled { opacity: .5; cursor: default; }
.shot-confirm button.danger { background: var(--danger); color: #fff; }
.shot-confirm button.danger:hover { background: #8c1d17; }
.shot.on { background: var(--accent); }

.only-narrow, #scrim { display: none; }
header { overflow-x: auto; scrollbar-width: none; white-space: nowrap; }
header::-webkit-scrollbar { display: none; }
header > * { flex: none; }
header .grow { flex: 1 0 0; }

/* touch screens: fatter pin targets (the pitch is only 9 px, the extra stroke stays under half of it) */
@media (pointer: coarse) {
  .pin { stroke: rgba(0, 0, 0, .001); stroke-width: 5; }
  .handle { r: 8; }
  .part-resize { stroke: rgba(29, 43, 36, .25); stroke-width: 6; paint-order: stroke; }
  .wire-hit { stroke-width: 16; }
  #dock button.sw { width: 28px; height: 28px; }
  kbd { display: none; }
}

/* tablets and phones: the parts list becomes a slide-in drawer, the dock wraps on two rows */
@media (max-width: 860px) {
  body { grid-template-columns: 1fr; grid-template-rows: 60px 1fr; }
  .only-narrow { display: inline-block; }
  header { padding: 0 10px; gap: 8px; }
  header .logo, #status { display: none; }
  .namebox input { width: 120px; }
  aside {
    position: fixed; z-index: 20; top: 60px; bottom: 0; left: 0; width: min(340px, 88vw); margin: 0; border-radius: 0 18px 0 0;
    box-shadow: 4px 0 24px rgba(0, 0, 0, .3);
    transform: translateX(-105%); transition: transform .2s ease-out;
    padding-bottom: env(safe-area-inset-bottom);
  }
  body.parts-open aside { transform: none; }
  body.parts-open #scrim { display: block; position: fixed; z-index: 10; inset: 60px 0 0 0; background: rgba(0, 0, 0, .25); }
  main { margin: 0; border-radius: 0; grid-column: 1; }
  #palette { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  #dock { flex-wrap: wrap; justify-content: center; gap: 4px 8px; bottom: calc(10px + env(safe-area-inset-bottom)); border-radius: 24px; padding: 6px 10px; width: max-content; }
  #swatches { flex-wrap: wrap; max-width: 210px; }
  #sel-name, #tip { display: none; }
}
@media (max-width: 480px) {
  #palette { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  header { gap: 6px; }
  header button { padding: 0 14px; }
  #open-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .dlg-head h2 { display: none; }
}
@media (max-height: 480px) and (orientation: landscape) {
  body { grid-template-rows: 52px 1fr; }
  header { height: 52px; }
  header button, .namebox { height: 38px; }
  aside { top: 52px; }
  body.parts-open #scrim { inset: 52px 0 0 0; }
}

/* ---------- print: the circuit alone, on white paper (app.js fits it to the page in beforeprint) ---------- */
@media print {
  @page { margin: 12mm; }
  html, body { height: auto; background: #fff; }
  body { display: block; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  header, aside, #scrim, #dock, #seltools, #tip, #erc, dialog { display: none !important; }
  main { margin: 0; border-radius: 0; overflow: visible; background: none; }
  main::before { content: attr(data-title); display: block; margin-bottom: 6mm; font: 800 16pt/1 var(--font-title); color: #000; }
  #canvas { width: 100%; height: min(88vh, 23cm); }
  /* no mat, and nothing that only shows the state of the editor */
  #grid-bg, #grid-inch-bg, #wire-preview, #erc-layer, .wire-sel, .handle, .note-resize, .note-resize-hit, .part.selected .part-resize { display: none; }
  .part.selected .outline, .note.selected .note-outline { stroke: none; fill: none; }
  /* no sticker effect on paper: a filtered drawing is printed as a bitmap (Safari) or not at all (Firefox),
     and its white border would not show on white anyway. Without it the parts stay vector drawings. */
  #canvas [filter] { filter: none; }
}
