/* Cairn. Bedrock's buttons, in ember and sandstone: warm stone, amber light, teal for the connectors. Every target is
   at least 44px for a finger. Two looks: dark ("ember") and light ("sandstone"). */
:root, :root[data-theme=dark] {
  --bg: #15110f; --bg2: #1b1613; --panel: #221b17; --panel2: #2a221c; --line: #3a2f27; --line2: #4c3d32;
  --text: #f4ebe3; --muted: #b6a597; --dim: #7f6f63; --accent: #ff9f43; --accent-ink: #1d1206; --teal: #36cfc0;
  --bad: #ff6b6b; --ok: #8fd16a; --grid: rgba(255, 186, 120, .07); --shadow: 0 14px 34px rgba(0, 0, 0, .45);
  --wire: #8a7766; --sheet: #1f1915; color-scheme: dark;
}
:root[data-theme=light] {
  --bg: #f4ede4; --bg2: #efe6db; --panel: #fffaf4; --panel2: #f8efe4; --line: #e3d4c3; --line2: #d3bea7;
  --text: #2b2119; --muted: #775f4d; --dim: #a28d7b; --accent: #d4711c; --accent-ink: #fff; --teal: #0d8c80;
  --bad: #d63c3c; --ok: #3f8f2a; --grid: rgba(120, 80, 40, .09); --shadow: 0 12px 28px rgba(80, 50, 20, .16);
  --wire: #a8917c; --sheet: #fffaf4; color-scheme: light;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Inter, system-ui, sans-serif; }
body { overflow: hidden; -webkit-tap-highlight-color: transparent; overscroll-behavior: none; }
button { font: inherit; color: inherit; cursor: pointer; }
input, select { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line2); border-radius: 12px; padding: .7rem .8rem; width: 100%; min-height: 46px; }
input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.primary { background: var(--accent); color: var(--accent-ink); border: 1px solid var(--accent); border-radius: 14px; padding: .7rem 1.1rem; font-weight: 650; min-height: 46px; }
.ghost { background: transparent; border: 1px solid var(--line2); border-radius: 14px; padding: .6rem 1rem; min-height: 46px; }
.danger { background: transparent; border: 1px solid var(--bad); color: var(--bad); border-radius: 14px; padding: .6rem 1rem; min-height: 46px; }
.big { width: 100%; }

/* ------------------------------------------------------------ the top bar */
#top { position: fixed; inset: 0 0 auto 0; height: calc(58px + env(safe-area-inset-top)); padding: env(safe-area-inset-top) 10px 0; display: flex; align-items: center; gap: 6px;
  background: linear-gradient(180deg, var(--bg2), color-mix(in srgb, var(--bg2) 88%, transparent)); border-bottom: 1px solid var(--line); z-index: 30; backdrop-filter: blur(8px); }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 720; letter-spacing: -.02em; font-size: 1.12rem; margin-right: 6px; }
.brand img { width: 30px; height: 30px; border-radius: 9px; }
.spacer { flex: 1; }
.tb { width: 44px; height: 44px; border-radius: 12px; border: 1px solid var(--line2); background: var(--panel); display: grid; place-items: center; font-size: 1.15rem; }
.tb:disabled { opacity: .4; cursor: default; }
#btn-palette { display: none; font-size: 1.5rem; background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.status { display: flex; align-items: center; gap: 6px; font-size: .82rem; color: var(--muted); padding: 4px 10px; border-radius: 999px; border: 1px solid var(--line); white-space: nowrap; }
.status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.status.saving .dot { background: var(--accent); box-shadow: 0 0 8px var(--accent); }
.status.bad .dot { background: var(--bad); box-shadow: 0 0 8px var(--bad); }

/* ------------------------------------------------------------ the palette on the left */
#palette { position: fixed; top: calc(58px + env(safe-area-inset-top)); bottom: 0; left: 0; width: 236px; padding: 14px 12px calc(20px + env(safe-area-inset-bottom)); overflow-y: auto;
  background: var(--bg2); border-right: 1px solid var(--line); z-index: 20; }
#palette h2 { font-size: .74rem; text-transform: uppercase; letter-spacing: .12em; color: var(--muted); margin: 6px 4px 8px; }
#palette h2:not(:first-child) { margin-top: 20px; }
.pal-tip { font-size: .78rem; color: var(--dim); margin: -2px 4px 10px; }
.items { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.pal { position: relative; border: 1px solid var(--line2); border-radius: 16px; background: linear-gradient(180deg, var(--panel), var(--panel2)); min-height: 76px; padding: 10px 8px 8px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; touch-action: none; user-select: none; -webkit-user-select: none; overflow: hidden; }
.pal:before { content: ""; position: absolute; inset: 0 0 auto 0; height: 3px; background: var(--c, var(--accent)); }
.pal .sym { font-size: 1.55rem; line-height: 1; font-weight: 700; color: var(--c, var(--accent)); }
.pal .lbl { font-size: .78rem; font-weight: 600; text-align: center; }
.pal.wide { grid-column: 1 / -1; flex-direction: row; justify-content: flex-start; gap: 10px; padding-left: 14px; }
.pal.wide .lbl { font-size: .92rem; }
.pal:active { transform: scale(.97); }
.pal-ghost { position: fixed; z-index: 60; pointer-events: none; opacity: .92; transform: translate(-50%, -50%) scale(1.06) rotate(1.5deg); box-shadow: var(--shadow); width: 104px; }
#scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, .4); z-index: 19; }

/* ------------------------------------------------------------ the board */
#stage { position: fixed; top: calc(58px + env(safe-area-inset-top)); left: 236px; right: 0; bottom: 0; overflow: hidden; touch-action: none;
  background-color: var(--bg); background-image: radial-gradient(var(--grid) 1.4px, transparent 1.5px); background-size: 26px 26px; }
#world { position: absolute; left: 0; top: 0; transform-origin: 0 0; }
#wires { position: absolute; left: 0; top: 0; overflow: visible; }
#nodes { position: absolute; left: 0; top: 0; }
.wire { fill: none; stroke: var(--wc, var(--wire)); stroke-width: 3.5; stroke-linecap: round; opacity: .9; }
.wire.pending { stroke-dasharray: 8 8; opacity: .7; animation: march 0.6s linear infinite; }
@keyframes march { to { stroke-dashoffset: -16; } }
.wire-hit { fill: none; stroke: transparent; stroke-width: 28; cursor: pointer; }
.wire.sel { stroke-width: 5.5; filter: drop-shadow(0 0 6px var(--wc, var(--wire))); }

.node { position: absolute; border: 1px solid var(--line2); border-radius: 20px; background: linear-gradient(180deg, var(--panel), var(--panel2));
  box-shadow: 0 6px 18px rgba(0, 0, 0, .22); user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; touch-action: none; transition: box-shadow .2s, border-color .2s; }
.node:before { content: ""; position: absolute; inset: 0 0 auto 0; height: 3px; border-radius: 20px 20px 0 0; background: var(--c, var(--accent)); }
.node .glow { position: absolute; inset: 0; border-radius: 20px; overflow: hidden; pointer-events: none; }
.node .glow:after { content: ""; position: absolute; width: 150px; height: 150px; right: -64px; top: -64px; border-radius: 50%; background: var(--c, var(--accent)); opacity: .1; filter: blur(12px); }
.node.sel { border-color: var(--c, var(--accent)); box-shadow: 0 0 0 3px color-mix(in srgb, var(--c, var(--accent)) 40%, transparent), var(--shadow); }
.node.moving { box-shadow: var(--shadow); cursor: grabbing; transition: none; }
.node .badge { position: absolute; top: -12px; left: 14px; background: var(--c, var(--accent)); color: #160d05; font-size: .72rem; font-weight: 800; border-radius: 999px; padding: 2px 8px; }
.node .more { position: absolute; top: 8px; right: 8px; width: 36px; height: 36px; border-radius: 10px; border: 1px solid var(--line2); background: rgba(0, 0, 0, .18); color: var(--muted); z-index: 2; }

.button { width: 188px; height: 136px; padding: 12px 12px 10px; }
.button .ico { font-size: 1.3rem; line-height: 1; height: 22px; }
.button .nm { font-weight: 640; font-size: .98rem; letter-spacing: -.01em; margin: 4px 44px 0 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.button .val { display: block; width: 100%; text-align: left; border: 0; background: transparent; padding: 0; margin-top: 2px; font-size: 1.9rem; font-weight: 750; letter-spacing: -.03em;
  font-variant-numeric: tabular-nums; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-height: 40px; }
.button .val.blank { font-size: 1rem; font-weight: 600; color: var(--dim); padding-top: 10px; }
.button .valedit { font-size: 1.5rem; font-weight: 700; padding: .25rem .5rem; min-height: 40px; margin-top: 2px; }
.button .steps { position: absolute; left: 10px; right: 26px; bottom: 9px; display: flex; align-items: center; gap: 6px; }
.button .steps button { width: 44px; height: 34px; border-radius: 11px; border: 1px solid var(--line2); background: rgba(0, 0, 0, .14); font-size: 1.25rem; font-weight: 700; line-height: 1; }
.button .steps .step { flex: 1; text-align: center; font-size: .72rem; color: var(--dim); }
.node.flash { animation: pulse .5s ease; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 var(--c); } 70% { box-shadow: 0 0 0 14px transparent; } 100% { box-shadow: 0 0 0 0 transparent; } }

.connector { width: 176px; height: 136px; padding: 12px 14px 10px 26px; --c: var(--teal); border-radius: 26px; }
.connector .op { display: flex; align-items: center; gap: 8px; margin-right: 40px; }
.connector .sym { font-size: 1.35rem; font-weight: 800; color: var(--c); width: 28px; height: 28px; border-radius: 9px; display: grid; place-items: center; background: color-mix(in srgb, var(--c) 16%, transparent); }
.connector .opname { font-size: .86rem; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.connector .result { font-size: 1.75rem; font-weight: 780; letter-spacing: -.03em; font-variant-numeric: tabular-nums; margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.connector .result.none { color: var(--dim); }
.connector .result.err { color: var(--bad); font-size: 1.3rem; }
.connector .formula { font-size: .76rem; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.connector .note { font-size: .7rem; color: var(--accent); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 1px; }
.connector .note.err { color: var(--bad); }

/* ports: a 44px target around a small dot */
.port { position: absolute; width: 44px; height: 44px; margin: -22px 0 0 -22px; display: grid; place-items: center; z-index: 3; touch-action: none; }
.port i { width: 16px; height: 16px; border-radius: 50%; border: 3px solid var(--c, var(--accent)); background: var(--panel); transition: transform .15s; }
.port.on i { background: var(--c, var(--accent)); }
.port span { position: absolute; left: -2px; font-size: .68rem; font-weight: 800; color: var(--c); transform: translateX(-100%); }
.port.can i { transform: scale(1.35); box-shadow: 0 0 0 6px color-mix(in srgb, var(--c, var(--accent)) 30%, transparent); animation: breathe 1s ease-in-out infinite; }
.port.from i { background: var(--c, var(--accent)); transform: scale(1.3); }
@keyframes breathe { 50% { box-shadow: 0 0 0 10px color-mix(in srgb, var(--c, var(--accent)) 12%, transparent); } }
.port.out { left: 100%; top: 50%; }
.port.a { left: 0; top: 36%; }
.port.b { left: 0; top: 72%; }

.wire-x { position: absolute; transform: translate(-50%, -50%); z-index: 4; background: var(--bad); color: #fff; border: 0; border-radius: 999px; padding: .5rem .9rem; font-weight: 700; min-height: 44px; box-shadow: var(--shadow); }

/* ------------------------------------------------------------ hints and bars */
#empty { position: absolute; left: 50%; top: 42%; transform: translate(-50%, -50%); width: min(440px, calc(100% - 32px)); text-align: center; color: var(--muted);
  background: color-mix(in srgb, var(--panel) 86%, transparent); border: 1px dashed var(--line2); border-radius: 22px; padding: 22px 22px 24px; }
#empty h1 { color: var(--text); font-size: 1.25rem; margin: 0 0 8px; }
#empty p { margin: 0 0 16px; }
#actionbar { position: absolute; left: 50%; bottom: calc(16px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 15; display: flex; gap: 6px; flex-wrap: wrap; justify-content: center;
  max-width: calc(100% - 20px); background: var(--sheet); border: 1px solid var(--line2); border-radius: 18px; padding: 8px; box-shadow: var(--shadow); }
#actionbar .lbl { align-self: center; font-size: .8rem; color: var(--muted); padding: 0 6px; }
#actionbar button { min-height: 44px; min-width: 44px; border-radius: 12px; border: 1px solid var(--line2); background: var(--panel); padding: 0 .8rem; font-weight: 600; }
#actionbar button.opchip { font-size: 1.15rem; font-weight: 800; color: var(--teal); padding: 0; }
#actionbar button.on { background: var(--teal); color: var(--bg); border-color: var(--teal); }
#actionbar button.danger { color: var(--bad); border-color: var(--bad); background: transparent; }
#toast { position: fixed; left: 50%; top: calc(70px + env(safe-area-inset-top)); transform: translateX(-50%); z-index: 70; display: flex; flex-direction: column; gap: 6px; pointer-events: none; width: min(520px, calc(100% - 24px)); }
#toast div { background: var(--sheet); border: 1px solid var(--line2); border-radius: 14px; padding: 10px 14px; box-shadow: var(--shadow); font-size: .9rem; animation: drop .2s ease-out; pointer-events: auto; }
#toast div.bad { border-color: var(--bad); }
@keyframes drop { from { transform: translateY(-6px); opacity: 0; } }

/* ------------------------------------------------------------ the sheet (editing, history, signing in) */
#sheet { position: fixed; right: 0; top: calc(58px + env(safe-area-inset-top)); bottom: 0; width: 360px; z-index: 40; background: var(--sheet); border-left: 1px solid var(--line2);
  box-shadow: var(--shadow); padding: 16px 16px calc(24px + env(safe-area-inset-bottom)); overflow-y: auto; }
#sheet h2 { margin: 0 0 12px; font-size: 1.1rem; display: flex; align-items: center; gap: 8px; }
#sheet h2 .x { margin-left: auto; }
#sheet label { display: block; font-size: .8rem; color: var(--muted); margin: 14px 0 6px; font-weight: 600; }
#sheet .row { display: flex; gap: 8px; align-items: center; }
#sheet .fine { font-size: .8rem; color: var(--dim); margin: 6px 0 0; }
#sheet .err { color: var(--bad); font-size: .82rem; min-height: 1em; margin-top: 4px; }
.swatches, .icons, .opgrid { display: flex; flex-wrap: wrap; gap: 8px; }
.swatch { width: 44px; height: 44px; border-radius: 12px; border: 2px solid transparent; background: var(--s); }
.swatch.on { border-color: var(--text); box-shadow: 0 0 0 2px var(--bg) inset; }
.iconpick { width: 44px; height: 44px; border-radius: 12px; border: 1px solid var(--line2); background: var(--panel); font-size: 1.25rem; }
.iconpick.on { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 18%, var(--panel)); }
.opgrid button { flex: 1 1 calc(50% - 8px); min-height: 52px; border-radius: 14px; border: 1px solid var(--line2); background: var(--panel); display: flex; align-items: center; gap: 10px; padding: 0 12px; text-align: left; }
.opgrid button b { color: var(--teal); font-size: 1.25rem; width: 22px; text-align: center; }
.opgrid button small { display: block; color: var(--dim); font-size: .7rem; }
.opgrid button.on { border-color: var(--teal); background: color-mix(in srgb, var(--teal) 14%, var(--panel)); }
.sheet-foot { display: flex; gap: 8px; margin-top: 22px; }
.sheet-foot > * { flex: 1; }
.rev { display: flex; align-items: center; gap: 10px; width: 100%; text-align: left; border: 1px solid var(--line); background: var(--panel); border-radius: 14px; padding: 10px 12px; margin-bottom: 8px; min-height: 52px; }
.rev small { color: var(--muted); }
.code-show { font-size: 2.2rem; font-weight: 800; letter-spacing: .15em; text-align: center; padding: 14px; border: 1px dashed var(--accent); border-radius: 16px; margin: 10px 0; font-variant-numeric: tabular-nums; }

/* ------------------------------------------------------------ the sign-in page */
body.login { overflow: auto; display: grid; place-items: center; min-height: 100%; padding: 20px; }
.card { width: 100%; max-width: 420px; background: var(--panel); border: 1px solid var(--line); border-radius: 22px; padding: 26px 22px; box-shadow: var(--shadow); }
.card h1 { display: flex; align-items: center; gap: 10px; margin: 0 0 18px; font-size: 1.5rem; }
.card h1 img { width: 40px; height: 40px; border-radius: 12px; }
.card label { display: block; font-size: .85rem; color: var(--muted); margin: 18px 0 8px; }
.card input { font-size: 1.4rem; letter-spacing: .12em; text-align: center; margin-bottom: 12px; }
.card .fine, .fine { font-size: .8rem; color: var(--dim); }
.msg { color: var(--bad); }

/* ------------------------------------------------------------ phones: the palette slides in from the left */
@media (max-width: 760px) {
  #btn-palette { display: grid; }
  .brand span { display: none; }
  #btn-history, #btn-theme { display: none; }
  #palette { width: min(300px, 86vw); transform: translateX(-102%); transition: transform .22s ease; box-shadow: var(--shadow); }
  body.pal-open #palette { transform: none; }
  #stage { left: 0; }
  #sheet { left: 0; width: auto; top: auto; max-height: 82vh; border-left: 0; border-top: 1px solid var(--line2); border-radius: 22px 22px 0 0; }
  .status .txt { max-width: 92px; overflow: hidden; text-overflow: ellipsis; }
}
@media (max-width: 380px) { #btn-fit { display: none; } }
